views:

83

answers:

1

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?

+1  A: 

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)

Alconja