#encoding: utf-8
FactoryGirl.define do
factory :product do
end
factory :iPhone4, parent: :product do
name "iPhone 4"
price BigDecimal.new("5999")
end
factory :iPoad2, parent: :product do
name "iPoad 2"
price BigDecimal.new("3999")
end
factory :psP1, parent: :product do
name "psp1"
price BigDecimal.new("2000")
end
end
使用 Factory(:iPhone4) 时出现 NoMethodError Exception: undefined method `delete' for nil:NilClass
其它的 Factory 正常,难道是 Product 模型里出问题! 请各位帮忙 谢谢 !
Failure/Error: let(:iPhone4) { Factory :iPhone4 }
NoMethodError:
undefined method `delete' for nil:NilClass
# ./spec/controllers/inventory_controller_spec.rb:6:in `block (2 levels) in <top (required)>'
# ./spec/controllers/inventory_controller_spec.rb:20:in `block (3 levels) in <top (required)>'
@Rei6 行的代码 let(:iPhone4) { Factory :iPhone4 } 我在 it 里面 debugger 用 Factory(:iPhone4) 也是出现这种错误!
begin
# code that raises exception
rescue Exception => e
puts e.backtrace
end
把 7 楼的用例代码包裹在这个 begin 里面看看。
begin
it "should factory" do
@iphone = Factory :iPhone4
debugger
iphone.should_not be_nil, @iphone.errors.inspect
end
rescue Exception => e
debugger
puts e.backtrace
end
it "should factory" do
begin
@iphone = Factory :iPhone4
@iphone.should_not be_nil, @iphone.errors.inspect
rescue Exception => e
puts e.backtrace
end
end
(rdb:1) p e.backtrace
["/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/activerecord-3.2.1/lib/active_record/attribute_methods/write.rb:28:in `write_attribute'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/activerecord-3.2.1/lib/active_record/attribute_methods/dirty.rb:67:in `write_attribute'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/activerecord-3.2.1/lib/active_record/attribute_methods/write.rb:14:in `name='", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/factory_girl-2.6.1/lib/factory_girl/attribute_assigner.rb:14:in `block (2 levels) in object'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/factory_girl-2.6.1/lib/factory_girl/attribute_assigner.rb:13:in `each'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/factory_girl-2.6.1/lib/factory_girl/attribute_assigner.rb:13:in `block in object'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/factory_girl-2.6.1/lib/factory_girl/attribute_assigner.rb:12:in `tap'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/factory_girl-2.6.1/lib/factory_girl/attribute_assigner.rb:12:in `object'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/factory_girl-2.6.1/lib/factory_girl/strategy/create.rb:9:in `result'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/factory_girl-2.6.1/lib/factory_girl/factory.rb:48:in `run'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/factory_girl-2.6.1/lib/factory_girl/factory_runner.rb:23:in `run'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/factory_girl-2.6.1/lib/factory_girl/syntax/methods.rb:63:in `create'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/factory_girl-2.6.1/lib/factory_girl/syntax/vintage.rb:50:in `default_strategy'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/factory_girl-2.6.1/lib/factory_girl/syntax/vintage.rb:143:in `Factory'", "/Users/kaka/Documents/soft/neza/spec/models/inventory_spec.rb:19:in `block (3 levels) in <top (required)>'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/rspec-core-2.8.0/lib/rspec/core/example.rb:80:in `instance_eval'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/rspec-core-2.8.0/lib/rspec/core/example.rb:80:in `block in run'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/rspec-core-2.8.0/lib/rspec/core/example.rb:173:in `with_around_hooks'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/rspec-core-2.8.0/lib/rspec/core/example.rb:77:in `run'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:355:in `block in run_examples'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:351:in `map'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:351:in `run_examples'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:337:in `run'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:338:in `block in run'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:338:in `map'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/rspec-core-2.8.0/lib/rspec/core/example_group.rb:338:in `run'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:28:in `map'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:28:in `block in run'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/rspec-core-2.8.0/lib/rspec/core/reporter.rb:34:in `report'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:25:in `run'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:80:in `run_in_process'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:69:in `run'", "/Users/kaka/Documents/soft/neza/vendor/gems/ruby/1.9.1/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:10:in `block in autorun'"]```