更新到rspec3后测试中出现奇怪的错误

时间:2015-04-04 20:14:21

标签: redirect ruby-on-rails-4 rspec-rails rspec3

将项目更新为Rspec 3后,我在测试中遇到以下奇怪错误,我无法修复:

 undefined method `empty?' for 2:Fixnum
 # /opt/local/lib/ruby2.1/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:251:in `block in missing_keys'
 # /opt/local/lib/ruby2.1/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:251:in `select'
 # /opt/local/lib/ruby2.1/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:251:in `missing_keys'
 # /opt/local/lib/ruby2.1/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:231:in `optimized_helper'
 # /opt/local/lib/ruby2.1/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:220:in `call'
 # /opt/local/lib/ruby2.1/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:345:in `block (2 levels) in define_url_helper'
 # ./app/controllers/users_controller.rb:10:in `block in <class:UsersController>'

users_controller第10行包含重定向:

  redirect_to(user_path(current_user))

在每种情况下,current_user都是使用mock_model(User, login: User.make_token)创建的模拟用户对象,并且在错误消息中引用了ID值(在&#34;:Fixnum&#34;之前)。 / p>

包含对象的每个重定向都会发生这种情况。例子:

redirect_to current_user
redirect_to user_path(current_user)
redirect_to url_for([:admin, current_user, :edit])

如果我使用redirect_to user_path(current_user.id))中的对象ID,则不会发生错误。然而,然后,例如url_for也会中断,因为我无法使用url_for([@parent.id, @object.id, :edit])(这些对象不能仅从其ID中派生,但路径需要这些对象。)

仅当使用带有RSpec 3和Rails 4.2.1的模拟对象时才会发生这种情况。任何帮助将不胜感激,因为这使得很多测试现在都失败了。

1 个答案:

答案 0 :(得分:1)

Rspec 3 extracted mocks support进入rspec-activemodel-mocks gem,因此您可能需要将rspec-activemodel-mocks添加到Gemfile中,以便路由助手在规范中按预期运行。

相关问题