Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rhino Mock Expect

Why is the response below always null 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);
}
like image 373
Mike Flynn Avatar asked Jun 17 '11 20:06

Mike Flynn


1 Answers

Your expectation is not correct. You defined that you expect an empty string as parameter on GetResponse, but you pass in the value SSO_URL. So the expectation is not meet and null is returned instead.

You have two options to correct this

One way is to set IgnoreArguments() on the expectation

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

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

target.Expect(t => t.GetResponse("http://localhost")).Return(authResponse);
like image 58
Jehof Avatar answered Sep 27 '22 21:09

Jehof