使用RhinoMocks 3.6更改存根方法参数?

时间:2013-12-06 21:41:44

标签: c# rhino-mocks

在下面的设计示例中,代码有条件地更新了提供的贷款的状态。使用RhinoMocks 3.6,我如何更改测试以使loan.LOAN_STATUS属性作为存根设置的一部分进行更新(而不是在存根设置范围之外将LOAN_STATUS更新为'A'以便将愚蠢的示例测试像我下面那样通过)?

[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 个答案:

答案 0 :(得分:2)

Hrmmmm,不知道这里的设计,但这是我要做的(我习惯了MOQ,所以语法可能有点偏离)

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