나는 테스트를 통과 할 수없는 rspec/공장 여자 테스트를 가지고있다.Rspec 공장 여자 문제
나는 current_user가 현재 로그인 한 사용자 모델을 호출하는 곳에 devise를 사용하고 있습니다.
나는
u = Factory(:user)
u.company
에서 테스트 콘솔과 유형을로드 할 수 있습니다 그리고 이것은 유효한 회사를 반환하지만 RSpec에 호출 current_user.company에서 어떤 이유로 전무를 반환합니다.
아이디어가 있으십니까?
컨트롤러
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
테스 t
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)>'
I는 f.company = 공장 (제거한
EDIT : 안돼요 ny)를 팩토리 파일에 추가하십시오. 분명히 존재하지 않는 @company
변수 인스턴스에 대한 검사 (기업) 그리고이
는 잘 모르겠어요하지만 양수인 생각 '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