如何使用ruby中的factory_girl测试规范

时间:2012-02-29 05:54:42

标签: ruby-on-rails factory-bot specifications

我尝试用factory_girl运行我的规范。我的app / models / student_spec.rb如下.., 需要'spec_helper'

describe Student do
   it "should work" do
     Factory.create(:name => 'User2', :age => '22', :department => 'IT')
   end
end

但它会抛出错误

Failures:

  1) Student should work
     Failure/Error: Factory.create(:name => 'User2', :age => '22', :department =
> 'IT')
     NoMethodError:
       undefined method `to_sym' for {:name=>"User2", :age=>"22", :department=>"
IT"}:Hash
     # ./spec/models/student_spec.rb:7:in `block (2 levels) in <top (required)>'


Finished in 0.00073 seconds
1 example, 1 failure

spec / factories.rb给出 要求'factory_girl'

Factory.define :student do |s|
    s.name 'user2'
    s.age '22'
    s.department 'IT'
end

我正在尝试使用spec.Either从数据库访问记录来创建/更新或从数据库中查找记录。但它对我不起作用。

1 个答案:

答案 0 :(得分:1)

您需要传递工厂名称。

Factory.create(:student, :name => 'User2', :age => '22', :department => 'IT')

虽然除了大写“user2”之外,它看起来不像你的出厂默认值一样。

您可以使用Factory(:student)