Change stubbed method argument with RhinoMocks 3.6?

124 views Asked by At

In the following contrived example, the code conditionally updates the provided loan's status. Using RhinoMocks 3.6, how might I change the test so the loan.LOAN_STATUS property is updated as part of the stub setup (rather than updating the LOAN_STATUS to 'A' outside the scope of the stub setup in order to get the silly example test to pass as I did below)?

[TestFixture]
public class RhinoMocksSpike : TestBase
{
    [Test]
    public void Update_ReferenceType_Property_Via_Stub()
    {
        var loan = new Domain.Loan { LOAN_STATUS = 'X' };

        var loanStatusUpdater = MockRepository
            .GenerateStub<ILoanStatusUpdater>();

        // How can I simulate the Loan Status updated
        // via the stub setup below?
        loan.LOAN_STATUS = 'A'; 

        loanStatusUpdater
            .Stub(x => x.TryUpdateStatus(loan))
            .Return(true);

        loanStatusUpdater.TryUpdateStatus(loan).ShouldBeTrue();
        loan.LOAN_STATUS.ShouldEqual('A');
    }

    public interface ILoanStatusUpdater
    {
        bool TryUpdateStatus(Domain.Loan loan);
    }

    public class LoanStatusUpdater : ILoanStatusUpdater
    {
        public bool TryUpdateStatus(Domain.Loan loan)
        {
            if (loan.LOAN_STATUS == 'X')
            {
                loan.LOAN_STATUS = 'A';
                return true;
            }

            return false;
        }
    }
}
1

There are 1 answers

1
Justin Pihony On BEST ANSWER

Hrmmmm, not sure about the design here, but here is what I would do (I am used to MOQ, so the syntax might be a little off)

loanStatusUpdater
        .Stub(x => x.TryUpdateStatus(loan))
        .WhenCalled(call => ((Loan)call.Arguments[0]).LOAN_STATUS = "A");                         
        .Return(true);