Using Rhino Mocks, How to Set a Parameter Property for a Mocked Method

Using the new Rhino Mocks 3.5 Arrange / Act / Assert (AAA) testing style, I am having trouble writing a test.

I have a method that calls a method in a repository class. ActivateFoo, where my Foo object has an IsActive property. The result of the ActivateFoo object should change the property.

Here is a sample code:

[TestMethod]
public void Should_update_foo_to_active_inside_of_repository()
{
  // arrange
  var repo = MockRepository.GenerateMock<IRepository>();
  var foo = new Foo() { ID = 1, IsActive = false };
  var target = new Presenter(repo);
  repo.Expect(x => x.ActivateFoo(foo)).Return(true);

  // act
  target.Activate(foo);

  // assert
  Assert.IsTrue(foo.IsActive);
  repo.VerifyAllExpectations();  
}

I assume that the key piece of code will be inbetween "ActivateFoo (foo))". and "Return (true)".

One moment to clarify how the chain of method elements works behind the scenes. If there is code written in the line that I expect, does it matter if it's after Return () or earlier? (unless, of course, the solution does not use MethodOptions Expect overload or something else).

Thanks in advance for your help.

+3
4

, - , Do. , ActivateFoo . ActivateFoo bool.

    [TestMethod]
    public void Should_update_foo_to_active_inside_of_repository()
    {
        // arrange
        var repo = MockRepository.GenerateMock<IRepository>();
        var foo = new Foo() { ID = 1, IsActive = false };
        var target = new Presenter(repo);
        repo.Expect(x => x.ActivateFoo(foo)).
            Do(new ActivateFooDelegate(ActivateFooDelegateInstance));
        // act
        target.Activate(foo);

        // assert
        Assert.IsTrue(foo.IsActive);
        repo.VerifyAllExpectations();
    }

    private delegate bool ActivateFooDelegate(Foo f);

    public bool ActivateFooDelegateInstance(Foo f)
    {
        f.IsActive = true;
        return f.IsActive;
    }
0

AB Kolan , .

[TestMethod]
public void Should_update_foo_to_active_inside_of_repository()
{
    // arrange
    var repo = MockRepository.GenerateMock<IRepository>();
    var foo = new Foo() { ID = 1, IsActive = false };
    var target = new Presenter(repo);
    repo.Expect(x => x.ActivateFoo(foo)).
        Do(new Func<Foo, bool>(
            delegate(Foo f) { f.IsActive = true; return true; }
        ));

    // act
    target.Activate(foo);

    // assert
    Assert.IsTrue(foo.IsActive);
    repo.VerifyAllExpectations();
}

, , , , , .

, , . , target.Activate(). Activate() ActivateFoo(), .

, , , .

+1

RhinoMocks, .Do( ), ( .Return).

, , , , .

0

In appearance, ActivateFoo should be a void method. And since you are mocking this, you should not check that it is changing anything on your object.

You would verify that the IsActive property changes when testing your ActivateFoo repository method, and not when testing the Activate method on a presenter.

0
source

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


All Articles