Mock.Verify() failed - This setup was not matched? - c#

I have the following test code.
var test = "Test";
var command = new MyCommand { V = test };
var mock = new Mock<IRepository>(); // IRepository has the method of Save()
var p = new P(test);
mock.Setup(x => x.Save(p)).Verifiable();
var sut = new C(mock.Object);
var result = await sut.M(command);
mock.Verify();
The test should pass. However, it failed with the error of,
Message:
Moq.MockException : Mock:
This mock failed verification due to the following:
IRepository x => x.Save(P):
This setup was not matched.
Stack Trace:
Mock.Verify()
sut.M() will convert a string X to type P with value of P(X).

It seems to me that you want to verify that the Save method from your mock is called with a specific value, and not just a type.
I have tried something like the following and believe it should work. I have modified your example.
var test = "Test";
var command = new MyCommand { V = test };
var mock = new Mock<IRepository>(); // IRepository has the method of Save()
var p = new P(test);
mock.Setup(x => x.Save(It.IsAny<P>());
var sut = new C(mock.Object);
var result = await sut.M(command);
mock.Verify(x => x.Save(It.Is<P>(v => v.Value.Equals(p.Value))), Times.AtLeastOnce);
This tests that the values of the specific property are equal.
I Tested this with the following test:
var test = "Test";
var mock = new Mock<ITestRepository>(); // ITestRepository has the method of Save()
var p = new P(test);
mock.Setup(x => x.Save(It.IsAny<P>()));
mock.Object.Save(new P(test));
mock.Verify(x => x.Save(It.Is<P>(v => v.Value.Equals(p.Value))), Times.AtLeastOnce);

Related

Unit testing a query to a CRM database with Nunit

I have the following method that I am trying to write unit tests for:
public Entity GetContactByEmail(string email)
{
var queryExpression = new QueryExpression(CRMFieldNames.Contact.EntityName);
queryExpression.ColumnSet = new ColumnSet(CRMFieldNames.Contact.EmailAddress1);
queryExpression.Criteria.AddCondition(CRMFieldNames.Contact.EmailAddress1, ConditionOperator.Equal, email);
var entities = _crmExecutor.Execute(service => service.RetrieveMultiple(queryExpression));
if (entities.Entities.Count > 0)
{
return entities.Entities[0];
}
return entities.Entities.FirstOrDefault();
}
I have attempted the following unit test:
[Test]
public void GetContactByEmail_AnyCase_ReturnsEntity()
{
var email = "lorem#ipsum.com";
var repository = CreateEmailSendRepository();
var query = new QueryExpression("contact");
var entityCollection = new EntityCollection();
entityCollection.Entities.Add(new Entity());
_crmExecutor.Execute(_organizationService => _organizationService.RetrieveMultiple(query)).Returns(entityCollection);
var result = repository.GetContactByEmail(email);
Assert.IsInstanceOf<Entity>(result);
}
private EmailSendRepository CreateEmailSendRepository()
=> new EmailSendRepository(_crmExecutor);
It is failing on the conditional in the GetContactByEmail method saying that entities = null. Can someone please point me in the right direction of giving entities a value here?

Unit Testing xUnit Asserting 2 Collections with Moq Interface

Good day everyone,
I'm new in xunit and even in unit testing. I have a code here and I'm trying to assert two collection of list. But I have no idea how to assert and pass this test. Here's my code
[Theory]
[InlineData(1)]
public void GetAllStudents_Exempt1(int number)
{
// arrange
var studentRepo = new Mock<IStudentRepository>();
var listOfStudents = new List<Student> { new Student { StudentId = 1, Firstname = "Firstname1", Lastname = "Firstname1" },
new Student{StudentId=2, Firstname="Firstname2",Lastname="Lastname2"} };
var getAllStudentDetailsExempt1 = studentRepo.Setup(s => s.GetStudents()).Returns(listOfStudents.Where(x => x.StudentId != number));
var studentService = new StudentService(studentRepo.Object);
// act
var getStudentsDetails = studentService.ListOfStudentsExempt1(1);
// assert
// I don't have any idea how to assert
}
First a few notes:
var getAllStudentDetailsExempt1 = studentRepo
.Setup(s => s.GetStudents())
.Returns(listOfStudents.Where(x => x.StudentId != number));
you don't need var getAllStudentDetailsExempt1, you can just setup your repo-mock...
studentRepo
.Setup(s => s.GetStudents())
.Returns(listOfStudents.Where(x => x.StudentId != number));
You probably want to change:
// act
var getStudentsDetails = studentService.ListOfStudentsExempt1(1);
to use the number variable...
// act
var getStudentsDetails = studentService.ListOfStudentsExempt1(number);
So then you can assert by checking some properties:
Assert.Equals(1, getStudentsDetails.Count);
Assert.Equals("FirstName1", getStudentsDetails.First().Firstname);
etc. etc.
Give it a shot!

Count files from request with Moq

The SetupGet for Form work, but the Count not work. How to resolve to Count return the value expected?
var httpContextMock = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();
httpContextMock.SetupGet(x => x.Request).Returns(request.Object);
httpContextMock.SetupGet(x => x.Request.Form).Returns(form);
httpContextMock.SetupGet(x => x.Request.Files.Count).Returns(2);
It is not working because you are setting up the wrong mock. Apply the setup on the request mock.
var httpContextMock = new Mock<HttpContextBase>();
var requestMock = new Mock<HttpRequestBase>();
requestMock.Setup(_ => _.Form).Returns(form);
requestMock.Setup(_ => _.Files.Count).Returns(2);
httpContextMock.Setup(_ => _.Request).Returns(requestMock.Object);
Just to prove the above works, I tested it like this
var context = httpContextMock.Object;
Assert.AreEqual(2, context.Request.Files.Count);
and it worked.
I did a quick test and it works if you access the request through the mock context.
[Test()]
public void Test()
{
var httpContextMock = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();
httpContextMock.SetupGet(x => x.Request).Returns(request.Object);
httpContextMock.SetupGet(x => x.Request.Files.Count).Returns(2);
var count = httpContextMock.Object.Request.Files.Count;
Assert.AreEqual(2, count);
}
As Nkosi suggested, however, you probably wanted to setup Files.Count on the requestMock itself.

callback is not called using moq + autofaq

I have a unit test done using moq to mock the objects, and the test is working fine, and now I want to use autofac +moq, but I'm having a few problems.
this is the test:
using (var mock = AutoMock.GetLoose())
{
var issues = new List<Issue>();
issues.Add(new Issue { Organization = "org", Repository = "repo", Number = 1 });
issues.Add(new Issue { Organization = "org", Repository = "repo", Number = 2 });
var numKeys = 0;
mock.MockRepository.Create<IStorageService>()
.Setup(myMock => myMock.GetBatchIssues(It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<IList<string>>()))
.Callback((string org, string repo, IList<string> keys) => numKeys = keys.Count)
.Returns(issues);
var sut = mock.Create<IssueReceiveService>();
var check = await sut.CheckInStorage("org", "repo", issues);
Assert.AreEqual(issues.Count, numKeys);
}
the call to sut.CheckInStorage return null, and the variable numKeys is not updated to the correct value. This test works fine using just moxk, so I suppose I'm missing something how to configure a mock with autoMock.
Where can I find more informations?
UPDATE:
after a few more tests I found the solution
using (var mock = AutoMock.GetLoose())
{
var issues = new List<Issue>();
issues.Add(new Issue { Organization = "org", Repository = "repo", Number = 1 });
issues.Add(new Issue { Organization = "org", Repository = "repo", Number = 2 });
var numKeys = 0;
mock.Mock<IStorageService>()
.Setup(myMock => myMock.GetBatchIssues(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IList<string>>()))
.Callback((string org, string repo, IList<string> keys) => numKeys = keys.Count)
.Returns(issues);
var sut = mock.Create<IssueReceiveService>();
var check = await sut.CheckInStorage("org", "repo", issues);
Assert.AreEqual(issues.Count, numKeys);
}
after a few more tests I found the solution
using (var mock = AutoMock.GetLoose())
{
var issues = new List<Issue>();
issues.Add(new Issue { Organization = "org", Repository = "repo", Number = 1 });
issues.Add(new Issue { Organization = "org", Repository = "repo", Number = 2 });
var numKeys = 0;
mock.Mock<IStorageService>()
.Setup(myMock => myMock.GetBatchIssues(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IList<string>>()))
.Callback((string org, string repo, IList<string> keys) => numKeys = keys.Count)
.Returns(issues);
var sut = mock.Create<IssueReceiveService>();
var check = await sut.CheckInStorage("org", "repo", issues);
Assert.AreEqual(issues.Count, numKeys);
}

Moq override previous setup?

Hi I am trying to set an mock of a class's method with two possible input. When i check output only the last set up return expected output. The first one did not. Any help is much appreciated.
[Test]
public void ClimbOnceTwoNeighbour_Sample()
{
stateConverter = new Mock<StateConverter>();
solution = new Mock<Solution>();
state = new Mock<State>();
var neightbourSolution1 = new Mock<Solution>();
var neighbourState1 = new Mock<State>();
var neightbourSolution2 = new Mock<Solution>();
var neighbourState2 = new Mock<State>();
stateConverter.Setup(x => x.FromSolution(neightbourSolution1.Object, It.IsAny<State>())).Returns(neighbourState1.Object);
stateConverter.Setup(x => x.FromSolution(neightbourSolution2.Object, It.IsAny<State>())).Returns(neighbourState2.Object);
var state1 = stateConverter.Object.FromSolution(neightbourSolution1.Object, state.Object);//return null ????
var state2 = stateConverter.Object.FromSolution(neightbourSolution2.Object, state.Object);//return neighbourState2.Object)
Assert.AreEqual(neighbourState2.Object, state2);//pass test here
Assert.AreEqual(neighbourState1.Object, state1);//fail here due to null is returned from previous statement
}
I have copied your code snippet and created empty classes to make it compile. It works as expected. Try it please and let me know what was the result.
Here is the code:
using Moq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var stateConverter = new Mock<StateConverter>();
var solution = new Mock<Solution>();
var state = new Mock<State>();
var neightbourSolution1 = new Mock<Solution>();
var neighbourState1 = new Mock<State>();
var neightbourSolution2 = new Mock<Solution>();
var neighbourState2 = new Mock<State>();
stateConverter.Setup(x => x.FromSolution(neightbourSolution1.Object, It.IsAny<State>())).Returns(neighbourState1.Object);
stateConverter.Setup(x => x.FromSolution(neightbourSolution2.Object, It.IsAny<State>())).Returns(neighbourState2.Object);
var state1 = stateConverter.Object.FromSolution(neightbourSolution1.Object, state.Object);
var state2 = stateConverter.Object.FromSolution(neightbourSolution2.Object, state.Object);
}
}
public class State{}
public class Solution{}
public abstract class StateConverter
{
public abstract State FromSolution(Solution p0, State isAny);
}
}
One of the habits I've got into with Moq is to use the full It.Is(o => o == object) syntax to avoid any issues when the setup could be ambiguous or implicit. It could be that Moq is simply taking the object in the setup and overriding any others it already had there.
stateConverter.Setup(x => x.FromSolution(neightbourSolution1.Object, It.IsAny<State>())).Returns(neighbourState1.Object);
stateConverter.Setup(x => x.FromSolution(neightbourSolution2.Object, It.IsAny<State>())).Returns(neighbourState2.Object);
Would then look like
stateConverter.Setup(x => x.FromSolution(It.Is<Solution>(solution => solution == neightbourSolution1.Object), It.IsAny<State>())).Returns(neighbourState1.Object);
stateConverter.Setup(x => x.FromSolution(It.Is<Solution>(solution => solution == neightbourSolution2.Object), It.IsAny<State>())).Returns(neighbourState2.Object);
I'm not too sure if this would solve your issue as the implicitness is pretty damn clear. :/
Have you tried something like this?
[Test]
public void ClimbOnceTwoNeighbour_Sample()
{
stateConverter = new Mock<StateConverter>();
solution = new Mock<Solution>();
state = new Mock<State>();
var neightbourSolution1 = new Mock<Solution>();
var neighbourState1 = new Mock<State>();
var neightbourSolution2 = new Mock<Solution>();
var neighbourState2 = new Mock<State>();
stateConverter.Setup(x => x.FromSolution(neightbourSolution1.Object, It.IsAny<State>())).Returns(neighbourState1.Object);
var state1 = stateConverter.Object.FromSolution(neightbourSolution1.Object, state.Object);//return null ????
stateConverter.Setup(x => x.FromSolution(neightbourSolution2.Object, It.IsAny<State>())).Returns(neighbourState2.Object);
var state2 = stateConverter.Object.FromSolution(neightbourSolution2.Object, state.Object);//return neighbourState2.Object)
Assert.AreEqual(neighbourState2.Object, state2);//pass test here
Assert.AreEqual(neighbourState1.Object, state1);//fail here due to null is returned from previous statement
}
I think that this way, when you assing to your state1 the result of the first return, you can use Setup again and add the result to state2 ;)

Categories

Resources