测试 rspec 使用 factory 出错! 求助

kaka · 2012年03月08日 · 最后由 kaka 回复于 2012年03月08日 · 4334 次阅读
#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 模型里出问题! 请各位帮忙 谢谢 !

具体是哪行调用了 delete?这个问题跟 rspec 应该无关。

#1 楼 @Rei 在 Product 模型与控制器我都查过,没有看到那里调用 delete

#2 楼 @kaka 异常的错误栈贴出来看看?

是不是 iPhone4 带有校验,生成时候没通过返回 nil,然后测试运行完被 delete 就抛错了。检查一下测试对象是否为 nil

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)>'

是不是这样的错误信息啊! #3 楼 @Rei

#4 楼 @Rei 我刚才把 Product 模型与控制器内容全注释,还是一样! 这个问题 git 分支合并的时候出现,之前没有合并分支的时候还没有问题!

#6 楼 @kaka ./spec/controllers/inventory_controller_spec.rb:6 这行代码打出来看看

分支合并是引进了什么代码?

先不用 let,直接写个用例

it "should factory" do
  @iphone = Factory :iPhone4
  @iphone.should_not be_nil, @iphone.errors.inspect
end

@Rei6 行的代码 let(:iPhone4) { Factory :iPhone4 } 我在 it 里面 debugger 用 Factory(:iPhone4) 也是出现这种错误!

#7 楼 @Rei 我 checkout 之前的分支,测试可能通过,没有出错误

begin
  # code that raises exception
rescue Exception => e
  puts e.backtrace
end

把 7 楼的用例代码包裹在这个 begin 里面看看。

#10 楼 @Rei 你试了,放 begin 代码块不会执行

#11 楼 @kaka ……改成咋样了?

        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

#12 楼 @Rei

it "should factory" do
  begin
    @iphone = Factory :iPhone4
    @iphone.should_not be_nil, @iphone.errors.inspect
  rescue Exception => e
    puts e.backtrace
  end
end


#14 楼 @Rei

(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'"]``` 

#15 楼 @kaka 测试数据库的表里面有 name 和 price 这两个字段吗?

#17 楼 @kaka 我无法完成所有 bug track 了,接下来你自己研究一下吧。

比如不用 Factory,直接 Product.create! 看看。错误栈已经打印出来了,追踪一下出问题的是哪个环节。

PS:rescue 那里加多个 puts e.message

#18 楼 @Rei 好的,谢谢你!

问题已经解决,是其它地方重写了 Product 类

需要 登录 后方可回复, 如果你还没有账号请 注册新账号