C# 犀牛

C# 犀牛,c#,.net,unit-testing,rhino-mocks,C#,.net,Unit Testing,Rhino Mocks,为什么在我的测试中下面的响应总是空的 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)

为什么在我的测试中下面的响应总是空的

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);
}
[TestMethod()]
public void Authenticate_Expired_ReturnTrue()
{
var target=MockRepository.GenerateStub();
AuthenticateResponse authResponse=new AuthenticateResponse(){Expired=true};
Expect(t=>t.GetResponse(“”).Return(authResponse);
target.Replay();
var response=target.Authenticate(“mflynn”、“password”);
Assert.IsTrue(response.Expired);
}

您的期望不正确。您定义了一个空字符串作为GetResponse的参数,但传入了值SSO_URL。因此,期望值不满足,而是返回null

您有两个选项来更正此问题

一种方法是对期望值设置IgnoreArguments()

target.Expect(t => t.GetResponse("")).IgnoreArguments().Return(authResponse);
另一种方法是将SSO_URL作为参数传递给GetResponse方法,如下所示

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

如果您仍在使用record/replay,请参阅。没错,我没有想到这一点