私はテストに合格できないrspec /工場の女の子のテストをしています。Rspec工場の女の子の問題
私はcurrent_userが現在ログインしているユーザーモデルを呼び出すdeviseを使用しています。
は私が
u = Factory(:user)
u.company
にテストコンソールとタイプをロードすることができますそして、これは有効な会社を返しますが、RSpecの呼び出しcurrent_user.companyで何らかの理由でnilを返しています。
アイデア?
コントローラ
class CompaniesController < ApplicationController
before_filter :authenticate_user!
def show
@company = current_user.company
end
end
モデル
class User < ActiveRecord::Base
validates_uniqueness_of :email, :case_sensitive => false
has_one :company
end
工場
Factory.define :company do |f|
f.name 'Test Company'
end
Factory.sequence(:email) do |n|
"person#{n}@example.com"
end
Factory.define :user do |f|
f.name 'Test User'
f.email {Factory.next :email}
f.password 'password'
f.company Factory(:company)
end
TESトン
describe CompaniesController do
before(:each) do
@user = Factory(:user)
sign_in @user
end
describe "GET show" do
before do
get :show
end
it "should find the users company" do
assigns(:company).should be_a(Company)
end
end
end
スペックヘルパー
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.infer_base_class_for_anonymous_controllers = false
end
テスト結果
Failures:
1) CompaniesController GET show should find the users company
Failure/Error: assigns(:company).should be_a(Company)
expected nil to be a kind of Company(id: integer, name: string, user_id: integer, created_at: datetime, updated_at: datetime)
# ./spec/controllers/companies_controller_spec.rb:21:in `block (3 levels) in <top (required)>'
EDIT
私はf.company =工場(削除されました:コンパをny)を指定します。そして、私のコントローラの仕様この
「spec_helper」
describe CompaniesController do
let(:current_user) { Factory(:user) }
before(:each) do
sign_in current_user
current_user.company = Factory(:company)
current_user.save
end
describe "GET show" do
before do
get :show
end
it "should find the users company" do
current_user.should respond_to(:company)
assigns(:company).should == current_user.company
end
end
end
前のブロックでletを使うことはできないと思います。 私はこの方向に行った、私は私のコードを私のメインポストに追加します – Marklar