单元测试 Monorail 的 RedirectToReferrer()

发布于 2024-07-05 21:36:24 字数 115 浏览 9 评论 0原文

我正在尝试为调用 Controller.RedirectToReferrer() 方法的操作方法编写单元测试,但收到“没有可用的引用者”消息。

我如何隔离和模拟这个方法?

I am trying to write a unit test for an action method which calls the Controller.RedirectToReferrer() method, but am getting a "No referrer available" message.

How can I isolate and mock this method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千柳 2024-07-12 21:36:24

在我正在使用的主干版本 r5299 中,我必须这样做才能模拟 RedirectToReferrer。 我认为它在最近的提交中发生了变化,我不确定。

[TestFixture]
public class LoginControllerTests : GenericBaseControllerTest<LoginController>
{
    private string referrer = "http://www.example.org";
    protected override IMockRequest BuildRequest()
    {
        var request = new StubRequest(Cookies);
        request.UrlReferrer = referrer;

        return request;
    }

    protected override IMockResponse BuildResponse(UrlInfo info)
    {
        var response = new StubResponse(info,
                                        new DefaultUrlBuilder(),
                                        new StubServerUtility(),
                                        new RouteMatch(),
                                        referrer);
        return response;
    }

奇怪的是

,您需要扰乱响应才能使RedirectToReferrer正常工作。 我不得不在单轨列车的来源中爬行才能找到答案。

In my version of the trunk I'm working against, r5299, I had to do this to mock out RedirectToReferrer. I think it's been changed in recent commits, I'm not sure.

[TestFixture]
public class LoginControllerTests : GenericBaseControllerTest<LoginController>
{
    private string referrer = "http://www.example.org";
    protected override IMockRequest BuildRequest()
    {
        var request = new StubRequest(Cookies);
        request.UrlReferrer = referrer;

        return request;
    }

    protected override IMockResponse BuildResponse(UrlInfo info)
    {
        var response = new StubResponse(info,
                                        new DefaultUrlBuilder(),
                                        new StubServerUtility(),
                                        new RouteMatch(),
                                        referrer);
        return response;
    }

etc. etc.

It's oddly the Response that you need to molest to get the RedirectToReferrer to work. I had to crawl around in the monorail sources to figure it out.

苦行僧 2024-07-12 21:36:24

您是否考虑过创建测试替身

Have you thought about creating a test double?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文