更新到 rspec3 后测试中的奇怪错误
Strange errors in tests after updating to 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 值(在“:Fixnum”之前)。
每个包含 对象 的重定向都会发生这种情况。示例:
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 中使用模拟对象时才会发生这种情况。任何帮助将不胜感激,因为这会导致很多测试立即失败。
Rspec 3 extracted mocks support into the rspec-activemodel-mocks gem,因此您可能需要将 rspec-activemodel-mocks
添加到您的 Gemfile 中,以便路由助手按照您的规范中的预期运行。
将项目更新到 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 值(在“:Fixnum”之前)。
每个包含 对象 的重定向都会发生这种情况。示例:
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 中使用模拟对象时才会发生这种情况。任何帮助将不胜感激,因为这会导致很多测试立即失败。
Rspec 3 extracted mocks support into the rspec-activemodel-mocks gem,因此您可能需要将 rspec-activemodel-mocks
添加到您的 Gemfile 中,以便路由助手按照您的规范中的预期运行。