为什么下面的响应在我的测试中总是空的?
单点登录
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);
}
最佳答案
您的期望不正确。您定义了您期望空字符串作为 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);
关于c# - Rhino 模拟期望,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6391655/