【问题标题】:RSpec includes help neededRSpec 包括所需的帮助
【发布时间】:2011-05-17 23:40:41
【问题描述】:

我试图模仿类似于http://relishapp.com/rspec/rspec-rails/v/2-2/dir/routing-specs/access-to-named-routes-in-routing-specs的东西

我有一个单元测试通过了:

    require 'test_helper'

    class RoutesTest < ActionController::TestCase
      test "book name is sent to store#index' do
        assert_routing 'book/mytitle', {:controller => 'book', :action => 'index', :title => 'mytitle'}
      end    
    end

我正在尝试将此转换为 RSpec 测试(在 Rails3.0.3 下运行 Rspec 2.2)

这是测试:

    require 'spec_helper'
    include RSpec::Rails::Matchers::RoutingMatchers
    include ActionDispatch::Assertions::RoutingAssertions

    describe "book specific routes" do
       it "should recognize title in path" do
         {:get => "book/mytitle"}.should route_to(:controller => "book", :action => "index", :title => "mytitle")
       end
    end

但这会导致:

Failures:

  1) book specific routes should recognize title in path
     Failure/Error: {:get => "book/mytitle"}.should route_to(:controller => "book", :action => "index", :title => "mytitle")
     undefined method `recognize_path' for nil:NilClass
     # ./spec/route_spec.rb:9:in `block (2 levels) in <top (required)>'

知道 nilClass 是从哪里来的吗?任何帮助将不胜感激。

【问题讨论】:

    标签: ruby-on-rails include rspec require


    【解决方案1】:

    这是导致失败的ActionDispatch::Assertions::RoutingAssertions 的双重包含——不知道为什么。删除两个include 语句,一切都会好起来的。规范文件应位于/spec/routing。您可以使用 describe BooksController 将示例包装起来作为样式点,但没有它也可以。

    【讨论】:

      【解决方案2】:

      我猜匹配器需要在控制器规范中使用。它们应该已经存在,因此无需手动包含它们。只需确保您描述的是控制器即可。

      describe BooksController do
        it "should recognize title in path" do
          # ...
        end
      end
      

      【讨论】:

        猜你喜欢
        • 2011-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-01
        • 2011-03-08
        • 1970-01-01
        相关资源
        最近更新 更多