使用 RSpec 设备测试模型
我在 Rails 应用程序中使用设备进行身份验证,但无法使用 RSpec 测试任何模型。控制器规格工作正常,但我无法使模型规格工作。
我收到此错误:
Failures:
1) Group should create new group
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `env' for nil:NilClass
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/whiny_nil.rb:48:in `method_missing'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/devise-1.4.2/lib/devise/test_helpers.rb:25:in `setup_controller_for_warden'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-rails-2.5.0/lib/rspec/rails/adapters.rb:11:in `block (2 levels) in setup'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/hooks.rb:29:in `instance_eval'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/hooks.rb:29:in `run_in'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/hooks.rb:64:in `block in run_all'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/hooks.rb:64:in `each'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/hooks.rb:64:in `run_all'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/hooks.rb:110:in `run_hook'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:191:in `block in eval_before_eachs'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:191:in `each'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:191:in `eval_before_eachs'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:144:in `run_before_each'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:48:in `block (2 levels) in run'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:106:in `with_around_hooks'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:46:in `block in run'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:99:in `block in with_pending_capture'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:98:in `catch'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:98:in `with_pending_capture'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:45:in `run'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:262:in `block in run_examples'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:258:in `map'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:258:in `run_examples'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:232:in `run'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/command_line.rb:27:in `block (2 levels) in run'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/command_line.rb:27:in `map'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/command_line.rb:27:in `block in run'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/reporter.rb:12:in `report'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/command_line.rb:24:in `run'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/runner.rb:55:in `run_in_process'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/runner.rb:46:in `run'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/runner.rb:10:in `block in autorun'
我已包含
config.include Devise::TestHelpers
在我的 spec_helper.rb 中。 这是我的模型测试文件
require 'spec_helper'
describe Group do
before :each do
@group = Factory.build(:group)
end
it "should create new group" do
Group.create!(@group)
end
end
这是我的factory.rb 文件
Factory.define :group do |group|
group.name "Example group"
group.description "Example description"
end
如何解决这个问题?
I am using devise for authentication in my rails app and I'm unable to test any model with RSpec. Controller specs work fine, but I'm unable to make model specs work.
I am getting this error:
Failures:
1) Group should create new group
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `env' for nil:NilClass
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/whiny_nil.rb:48:in `method_missing'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/devise-1.4.2/lib/devise/test_helpers.rb:25:in `setup_controller_for_warden'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-rails-2.5.0/lib/rspec/rails/adapters.rb:11:in `block (2 levels) in setup'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/hooks.rb:29:in `instance_eval'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/hooks.rb:29:in `run_in'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/hooks.rb:64:in `block in run_all'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/hooks.rb:64:in `each'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/hooks.rb:64:in `run_all'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/hooks.rb:110:in `run_hook'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:191:in `block in eval_before_eachs'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:191:in `each'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:191:in `eval_before_eachs'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:144:in `run_before_each'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:48:in `block (2 levels) in run'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:106:in `with_around_hooks'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:46:in `block in run'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:99:in `block in with_pending_capture'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:98:in `catch'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:98:in `with_pending_capture'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:45:in `run'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:262:in `block in run_examples'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:258:in `map'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:258:in `run_examples'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:232:in `run'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/command_line.rb:27:in `block (2 levels) in run'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/command_line.rb:27:in `map'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/command_line.rb:27:in `block in run'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/reporter.rb:12:in `report'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/command_line.rb:24:in `run'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/runner.rb:55:in `run_in_process'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/runner.rb:46:in `run'
# /home/hrvoje/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/runner.rb:10:in `block in autorun'
I have included
config.include Devise::TestHelpers
in my spec_helper.rb.
Here is my model test file
require 'spec_helper'
describe Group do
before :each do
@group = Factory.build(:group)
end
it "should create new group" do
Group.create!(@group)
end
end
This is my factories.rb file
Factory.define :group do |group|
group.name "Example group"
group.description "Example description"
end
How to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)