Here's another rookie noob question here:
Consider the following:
public class Company
{
public string CompanyName {get; set; }
public string Address {get; set; }
}
public interface IRepository
{
bool CreateDatabase(string name);
bool UpdateTable(Company _co);
}
public class MyService
{
private IRepository _repo;
public MyService(IRepository repo)
{
_repo = repo;
}
public bool Commit(string database_name, Company co)
{
if (_repo.CreateDatabase(database_name))
return _repo.Commit(co);
return false;
}
}
So in the repository, if it was successful on creating the database, then it returns TRUE. Same goes for the UpdateTable if all SQL UPDATE was successful as well. Then all I'm doing is passing along the reults back to ViewModel to indicate that all went well.
So in my MSTest:
[TestMethod]
public void Test_If_Calls_CreateDatabase_And_UpdateTable()
{
var name = "test";
var repoMock = new Mock<IRepository>();
var _company = new Company();
repoMock.Setup(r => r.CreateDatabase(It.IsAny<string>())).Returns(true);
repoMock.Setup(r => r.UpdateCompanyTable(It.IsAny<Company>())).Returns(true);
var engine = new MyService(repoMock.Object);
var result = engine.Commit(name, _company);
Assert.IsTrue(result); // PASSES
repoMock.Verify(r => r.CreateDatabase(name), Times.Once); //FAILS
repoMock.Verify(r => r.UpdateCompanyTable(_company), Times.Once); //FAILS
}
This is where things get so whacked. The result returns back true, which means that it got the proper results back, but the Moq verification fails and I get this exception:
Expected invocation on the mock at least once, but was never performed: r => r.CreateDatabase("test")
Why is that? Even if I remove Times.Once and just have repoMock.Verify(r => CreateDatabase(name))
I still get the same failed error message.
Any suggestions?
The code really was the first suggested answer. but there was some copy and paste errors that crept in and this was due to the fact I was trying some variations to make the unit testing to work. For example, I've tried these variations:
engine.Commit(name, _company); // tried that first, that failed
engine.Commit(It.IsAny<string>(), It.IsAny<Company>()); // failed too
engine.Commit(new String(), new Company()); // fails as well
engine.Commit(null, _company)); // fails!! GRR!!
engine.Commit(null, null)); // FAIL! *cocks gun* IT'S HAMMER TIME! *BANG!!*
I would have thought that the first line would have worked as someone suggested, but it didn't, hence why I am here asking for some help.
Actually, it was Chetan Ranpariya that came up with the answer in the comments, and I was hoping he would have posted a suggested answer instead of me so I could have given him the credit.
Because I'm using It.IsAny<string>()
and It.IsAny<Company>()
, I needed to have the SAME signature when trying to verify that the methods were indeed running once. Therefore, to test it out, it should have been this:
repoMock.Verify(r => r.CreateDatabase(It.IsAny<string>()), Times.Once);
repoMock.Verify(r => r.UpdateCompanyTable(It.IsAny<Company>()), Times.Once);
NOW the test passes.
Thanks Chetan!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With