【问题标题】:Rspec - redirect_to a path matching a regexpRspec - 重定向到匹配正则表达式的路径
【发布时间】:2013-12-19 11:35:43
【问题描述】:

我在 Rspec 中有以下内容

response.should redirect_to %r{\A/my_settings/mysub_path/}

我收到以下错误

NoMethodError: undefined method `model_name' for Regexp:Class

我只需要匹配包含 my_settings/mysub_path 的路径的一部分。 这在 rspec 中是如何实现的?

【问题讨论】:

    标签: ruby-on-rails rspec rspec2 rspec-rails


    【解决方案1】:

    要根据正则表达式检查重定向,请在 response.location 上进行字符串匹配,如下所示:

    expect(response.location).to match('your_expected_location.com')
    

    【讨论】:

      【解决方案2】:

      redirect_to 需要一个字符串或一个活动记录模型。

      如果你想匹配一些东西,你可以使用response.location

      【讨论】:

        【解决方案3】:

        这是另一种简单的方法:

        response.location.should =~ /whatever_you_want_to_match/
        

        【讨论】:

        • 我认为“应该”语法已被弃用,取而代之的是“期望”。
        猜你喜欢
        • 2020-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-19
        • 2022-08-11
        • 1970-01-01
        • 2021-08-31
        • 2011-11-19
        相关资源
        最近更新 更多