Mik*_*ynn 5 .net c# 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)
{
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; }
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
你的期望是不正确的.您定义了一个空字符串作为GetResponse上的参数,但您传入了值SSO_URL.因此期望不满足而是返回null.
您有两种方法可以解决此问题
一种方法是根据期望设置IgnoreArguments()
target.Expect(t => t.GetResponse("")).IgnoreArguments().Return(authResponse);
Run Code Online (Sandbox Code Playgroud)
另一种方法是将SSO_URL作为参数传递给GetResponse方法,就像这样
target.Expect(t => t.GetResponse("http://localhost")).Return(authResponse);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7012 次 |
| 最近记录: |