Rhino Mocks, VerifyAllExpectations

I would like to track a method call with Rhino Mocks. Suppose I have this code:

public class A { protected IB _b; public A(IB b) { _b = b; } public void Run( string name ) { _b.SomeCall(new C { Name = name }); } } public interface IB { void SomeCall( C c ); } public class C { public string Name { get; set; } // more attributes here } 

And the test looks like this:

 // prepare var bMock = Rhino.Mocks.MockRepository.GenerateStrictMock<IB>(); bMock.Expect(x => x.SomeCall(new C { Name = "myname" })); var sut = new A(bMock); // execute sut.Run("myname"); // assert bMock.VerifyAllExpectations(); 

The test fails with an ExpectedViolationException because the Rhino Mocks framework detects 2 different C classes.

How to test a call if an object under testing creates an object parameter in a test method? Any chance to tell Rhino Mocks to check the parameter as "Equals"?

Thank you, ton!

+6
source share
2 answers

I recommend that you use the much simpler (and more convenient) AAA syntax. In most cases, severe bullying hurts more than anything else.

arguments are compared using Equals . If C does not override Equals , it is compared by reference and will not match your case. Use Matches to check the argument in some other way.

 // arrange var bMock = MockRepository.GenerateMock<IB>(); var sut = new A(bMock); // act sut.Run("myname"); // assert bMock.AssertWasCalled(x => x.SomeCall(Arg<C>.Matches(y => y.Name == "myname")); 
+12
source

You need to add IgnoreArguments and optionally add a parameter to call "SomeCall":

 bMock.Expect(x => x.SomeCall(new C { Name = "myname" })) .IgnoreArguments() .Constraints(new PropertyConstraint(typeof(C), "Name", Rhino.Mocks.Constraints.Is.Equal("myname"))); 
+2
source

Source: https://habr.com/ru/post/923632/


All Articles