[英]How to test with spec with factory_girl in ruby
我嘗試使用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從數據庫訪問記錄,無論是創建/更新還是從數據庫中查找記錄,但這對我來說都不起作用。
您需要輸入工廠名稱。
Factory.create(:student, :name => 'User2', :age => '22', :department => 'IT')
盡管除了大寫“ user2”外,您似乎沒有覆蓋出廠默認設置。
您可以只使用Factory(:student)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.