Rhino Mock Expect

Why is the answer always always zero in my test?

SSO.cs

 public class SSO : ISSO
    {
        const string SSO_URL = "http://localhost";
        const string SSO_PROFILE_URL = "http://localhost";

        public AuthenticateResponse Authenticate(string userName, string password)
        {
            return GetResponse(SSO_URL);
        }

        public void GetProfile(string key)
        {
            throw new NotImplementedException();
        }

        public virtual AuthenticateResponse GetResponse(string url)
        {
            return new AuthenticateResponse();
        }
    }

    public class AuthenticateResponse
    {
        public bool Expired { get; set; }
    }

SSOTest.cs

 [TestMethod()]
public void Authenticate_Expired_ReturnTrue()
{
    var target = MockRepository.GenerateStub<SSO>();
    AuthenticateResponse authResponse = new AuthenticateResponse() { Expired = true };

    target.Expect(t => t.GetResponse("")).Return(authResponse);
    target.Replay();

    var response = target.Authenticate("mflynn", "password");


    Assert.IsTrue(response.Expired);
}
+5
source share
1 answer

Your expectation is wrong. You have determined that in GetResponse you expect an empty string as a parameter, but you pass the value SSO_URL. Thus, expectation does not occur, and a value is returned instead.

You have two options to fix this.

One way is to set IgnoreArguments () while waiting

target.Expect(t => t.GetResponse("")).IgnoreArguments().Return(authResponse);

and the other way is to pass in your SSO_URL as a parameter to the GetResponse method like this

target.Expect(t => t.GetResponse("http://localhost")).Return(authResponse);
+7
source

All Articles