c# - Rhino 模拟期望

标签 c# .net unit-testing rhino-mocks

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

单点登录

 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/

相关文章:

c# - 如何在用户控件完全可见后执行方法

c# - 在查询字符串中存储以前的 url 的巧妙方法 (ASP.NET C#)

c# - MessageBox.Show 的简单 "Login Attempt"问题

android - 是否可以在单元测试下模拟 android 服务?

c# - 如何使用客户端对象模型检索呈现的 Sharepoint WebPart 数据

.net - NHibernate 和 Windsor 程序集冲突

c# - 如何处理您想要扩展的类,该类密封在 .NET 库中?

c# - 类成员访问的点表示法

android - 在 Android 中测试非 Activity 类

javascript - 如何使用 Mocha 在 Visual Studio Code 中设置测试文件夹的目录?