ruby-on-rails - RSpec with Ruby on Rails - 测试路由时参数数量错误(1 代表 0)

标签 ruby-on-rails ruby ruby-on-rails-3 rspec tdd

我尝试测试路由,只是从 rspec-rails 文档中复制了示例。

describe "routing to profiles" do
  it "routes /profile/:username to profile#show for username" do
    expect(:get => "/profiles/jsmith").to route_to(
      :controller => "profiles",
      :action => "show",
      :username => "jsmith"
    )
  end
end

运行 RSpec 时出现以下错误:

Failures:

  1) routing to profiles routes /profile/:username to profile#show for username
     Failure/Error: expect(:get => "/profiles/jsmith").to route_to(
     ArgumentError:
       wrong number of arguments (1 for 0)
     # ./spec/routing/test_spec.rb:11:in `block (2 levels) in <top (required)>'

Finished in 0.001 seconds
1 example, 1 failure

这里出了什么问题?

感谢您的帮助。

最佳答案

expect 占用一个 block 。这意味着使用大括号:

expect{ :get => "/profiles/jsmith" }.to route_to(

引用:RSpec Expectations 2.0

我认为无论如何你都不需要期待。来自 Testing an RSpec controller action that can't be accessed directly 的代码

describe "routing" do
  it "routes /auth/:provider/callback" do
    { :post => "/auth/twitter/callback" }.should route_to(
      :controller => "authentications",
      :action => "create",
      :provider => "twitter")
  end
end

关于ruby-on-rails - RSpec with Ruby on Rails - 测试路由时参数数量错误(1 代表 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13212410/

相关文章:

ruby-on-rails - 来自模型的 expire_action

ruby-on-rails - Ruby on Rails : setting future "publishing" dates for blog postings

ruby - `each` 方法是否改变了其行为?

ruby-on-rails - 在 Mac 上安装 Postgres gem 时出错

ruby-on-rails - Rails : Sidekiq Workers must be thread-safe. 是什么意思?

ruby-on-rails - 覆盖Activerecord保存方法好吗?

ruby-on-rails-3 - 有没有办法在开发环境中 "rake db:migrate RAILS_ENV=test"之后自动有一个 "rake db:migrate"?

mysql - Rails 3 序列化哈希值保存但不持久

ruby-on-rails - 如何在 erb 文件中渲染嵌套集合 - ruby​​ on Rails

ruby-on-rails - 通过 Rails 3 中的唯一列值选择所有列