Rhinomocks 3.5 for dummies ... i.e. me

I am trying to use Rhinomocks 3.5 and the new lambda nomenclature to mock some benchmarks. I read this one but there are many more questions. Are there any complete examples, especially for the MVC architecture?

For example, what is the best way to mock this.

    public void OnAuthenticateUnitAccount()
    {
        if(AuthenticateUnitAccount != null)
        {
            int accountID = int.Parse(_view.GetAccountID());
            int securityCode = int.Parse(_view.GetSecurityCode());
            AuthenticateUnitAccount(accountID, securityCode);
        }
    }

      

There is a presentation interface and a presenter interface. It raises an event on the controller.

What I came up with is this.

[TestMethod()]
    public void OnAuthenticateUnitAccountTest()
    {
        IAuthenticationView view = MockRepository.GenerateStub<IAuthenticationView>();
        IAuthenticationPresenter target = MockRepository.GenerateMock<IAuthenticationPresenter>();

        target.Raise(x => x.AuthenticateUnitAccount += null, view.GetPlayerID(), view.GetSecurityCode());
        target.VerifyAllExpectations();
    }

      

It passes, but I don't know if it is correct.

And yes, we do tests after we developed ... it had to be done quickly.

0


source to share


1 answer


I am making the assumption that this is in one of your controllers. Also, I assume that you have a way to pass the view data through a constructor or setter, and that you have a way to register an AuthenticateUnitAccount handler. With that in mind, I would do something like the following:

[TestMethod]
public void OnAuthenticateUnitAccountSuccessTest()
{
    IAuthenticationView view = MockRepository.GenerateStub<IAuthenticationView>();
    view.Stub( v => GetPlayerID() ).Returns( 1 );
    view.Stub( v => GetSecurityCode() ).Returns( 2 );

    FakeAuthenticator authenticator = MockRepository.GenerateMock<FakeAuthenticator>();
    authenticator.Expect( a => a.Authenticate( 1, 2 ) );

    Controller controller = new Controller( view );
    controller.AuthenticateUnitAccount += authenticator.Authenticate;

    controller.OnAuthenicateAccount()

    authenticator.VerifyAllExpectations();
}

      

The FakeAuthenticator class contains an Authenticate method that matches your handler signature. Since you need to know if this method has been called, you need to mock it rather than stub it to make sure it is called with the correct arguments, etc. You will notice that I am directly calling the method and not raising the event, Since you only need to check your code here, there is no need to check what happens when the event occurs. You can check this elsewhere. Here, we just want to know that the correct methods are being called with the correct arguments.



For failure, you can do something like:

[TestMethod]
[ExpectedException(typeof(UnauthorizedException))]
public void OnAuthenticateUnitAccountFailureTest()
{
    IAuthenticationView view = MockRepository.GenerateStub<IAuthenticationView>();
    view.Stub( v => GetPlayerID() ).Returns( 1 );
    view.Stub( v => GetSecurityCode() ).Returns( 2 );

    FakeAuthenticator authenticator = MockRepository.GenerateMock<FakeAuthenticator>();
    authenticator.Expect( a => a.Authenticate( 1, 2 ) )
                 .Throw( new UnauthorizedException() );

    Controller controller = new Controller( view );
    controller.AuthenticateUnitAccount += authenticator.Authenticate;

    controller.OnAuthenicateAccount()

    authenticator.VerifyAllExpectations();
}

      

+2


source







All Articles