我写了以下示例:
it "should assign @services containing all the current user services" do
customer = FactoryGirl.create(:user, fullname: "Iris Steensma")
sign_in customer
service = FactoryGirl.create(:service, user: customer)
puts "Service.count = #{Service.count}" # Service.count = 0
get :home
assigns[:services].should eq([service])
end
动作控制器为:
def home
#@services = curent_user.posted_services
@services = Service.all
end
factory.rb 包含:
FactoryGirl.define do
sequence :address do |n|
"Street #{n}"
end
factory :user do
fullname "Foo Bar"
email { "#{fullname.gsub(' ', '.').downcase}@example.com" if fullname }
password "secret"
end
factory :preference do
profile "customer"
user
end
factory :service do
status :pending
source_addr { generate(:address) }
target_addr { generate(:address) }
passenger "Mis Daysi"
start_at Time.now
offer 5
payment "cash"
user
end
end
为什么 Factory Girl 无法创建服务记录?工厂在“rails c test”测试环境下工作正常
这是 rspec 输出:
Failures:
1) UsersController GET home should assign @services containing all the current user services
Failure/Error: assigns[:services].should eq([service])
expected: [#<Service:0x460d8ea @name="Service_1003">]
got: []
(compared using ==)
Diff:
@@ -1,2 +1,2 @@
-[#<Service:0x460d8ea @name="Service_1003">]
+[]
# ./spec/controllers/users_controller_spec.rb:26:in `block (3 levels) in <top (required)>'
Finished in 1.03 seconds
3 examples, 1 failure