Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Rhino Mock deeper/nested members directly?

Is it possible to mock a stub/mock's object member call without having to define that as a stub, and also set the return value as all seperate verbose lines?

Example:

    [TestMethod]
    public void AssignedPermissions_AssociateExists_ReturnsEdit_Rhino()
    {

       //Arrange
        var fakeConfiguration = MockRepository.GenerateStub<IDomainControllerConfiguration>();
         var fakeAssociateRepository = MockRepository.GenerateStub<IAssociateRepository>();
        fakeConfiguration.Stub(x => x.AssociateRepository).Return(fakeAssociateRepository);
        fakeAssociateRepository.Stub(x=>x.GetAssociatesByRole(null,false,null)).IgnoreArguments()
            .Return(new IAssociate[]{MockRepository.GenerateStub<IAssociate>()});

        var domain = new DomainController(fakeConfiguration);

        const AssignedPermission expected = AssignedPermission.Edit;

        //Act
        AssignedPermission actual = domain.AssignedPermissions();

        //Assert
        Assert.AreEqual(expected, actual);
    }

Are all those temporary variables necessary just to stub out nested method calls?

like image 975
Maslow Avatar asked Feb 02 '10 21:02

Maslow


1 Answers

I've never used the functionality, so I'm not 100% certain that this will work, but theoretically Rhino mocks supports "recursive mocking", which should allow you to at least cut out the fakeAssociateRepository by doing something like this:

var fakeConfiguration = MockRepository.GenerateStub<IDomainControllerConfiguration>();
fakeConfiguration.Stub(x => x.AssociateRepository.GetAssociatesByRole(null,false,null))
            .IgnoreArguments()
            .Return(new IAssociate[]{MockRepository.GenerateStub<IAssociate>()});

var domain = new DomainController(fakeConfiguration);

(note: code not tested, or even compiled)

like image 118
Alconja Avatar answered Oct 10 '22 23:10

Alconja