次の例を書きました。
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