Rhino Mocks: AAA Synax: Assert property set to type

I am trying to claim that the property in the mock object was set with the given type. The property has an abstract type and is specified by one of several specific types.

This is what I am trying to do, and it always passes the test regardless of the value that Foo.DoSomething () sets Foo.Bar with

[Test] public void DoSomething_SetsCorrectBar() { // Arrange Foo foo = MockRepository.GenerateMock<Foo>(); // Creates mock in Replay mode (what I want for AAA syntax). // Act foo.DoSomething(); // Assert that DoSomething set Foo.Bar to an instance of CorrectBarSubclass foo.AssertWasCalled(foo => foo.Bar = null, options => options.WhenCalled(invocation => Assert.That(invocation.Arguments[0] is CorrectBarSubclass))); } 

The Rhino 3.5 / AAA Documentation describes how to set expectations on property sets that have a given value, but I just want to check the value type.

As stated in the set of properties, in particular, on the set of properties having a given type of parameter?


Update: The above example is simplified. What I am actually testing is a separate state class. This is one of several states in which there may be a โ€œparent objectโ€ (an object having a state, Foo in this case). I checked that the checked state (let's call it BarOne) correctly installed Foo.State in the BarTwo instance when the time came for the transition states.

A clearer example (with the decision made):

  [Test] public void BarOne_DoSomething_SetsNextState() { // Arrange Foo foo = MockRepository.GenerateMock<Foo>(); // Creates mock in Replay mode (what I want for AAA syntax). foo.Stub(x => x.CreateBarTwoState()).Return(new BarTwo(foo)); BarOne bar = new BarOne(foo); // We are testing the BarOne state independently of Foo, that why we mock Foo but instantiate BarOne. // Act bar.DoSomething(); // Assert that DoSomething set Foo.Bar to an instance of BarTwo foo.AssertWasCalled(foo => foo.Bar = Arg<BarTwo>.Is.TypeOf); } 
+2
source share
2 answers

Maybe something like this:

 [Test] public void AddPlayer_GivesGameEnoughPlayersToStart_SetsNextState() { // Arrange Foo foo = MockRepository.GenerateMock<Foo>(); // Creates mock in Replay mode (what I want for AAA syntax). foo.Expect(m => m.Bar = Arg<CorrectBarSubclass>.Is.TypeOf); // Act foo.DoSomething(); //Assert foo.VerifyAllExpectations(); } 

So what's going on ..

We changed the statement as Pending. I find it a little cleaner, also waiting allows us a cleaner test of what type is. We say, โ€œWe expect Bar be installed in the instance of CorrectBarSubclass . Then we act and claim that our expectation is fulfilled.

A couple of things: every time you mock a class, all you have to do is call Expect or Stub should be virtual or abstract, so in this case Bar should be virtual. As a rule of thumb, itโ€™s always better to make fun of the interface and check how the class uses dependency, rather than test how the class uses itself (which is usually a sign of excessive testing or incorrect separation of problems).

In your case, is this even a layout? You simply use a somewhat complicated syntax to claim the result of real behavior, but nothing really mocks but the property setter. Sometimes it's just easier and, more appropriate, checking for real behavior. Why not do something like this:

 var foo = new Foo(); foo.DoSomething(); Assert.That(foo.Bar is CorrectBarSubclass); 
+4
source

I had a similar problem when I tried to drown out the Find-method predicate expression that contains a string (which, of course, is immutable). There is no success until I create a testPredicate, which I pass to the stub, the actual SUT and finally assert. Below is the code.

  [Test()] public void Search_Apartment_With_Spesific_Address() { //ARRANGE var repositoryMock = MockRepository.GenerateMock<IApartmentRepository>(); var notificationMock = MockRepository.GenerateMock<INotificationService>(); var service = new ApartmentService(repositoryMock, notificationMock); var apartment = new List<Apartment> {new Apartment {Address = "Elm Street 2"}}.AsQueryable(); Expression<Func<Apartment, bool>> testPredicate = a => a.Address == "Elm Street 2"; repositoryMock.Stub(x => x.Find(testPredicate)).Return(apartment); //ACT service.Find(testPredicate); //ASSERT repositoryMock.AssertWasCalled(x => x.Find(testPredicate)); } 

EDIT: This answer has been fixed and adjusted to fix the IQueryable Stub question.

+3
source

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


All Articles