2015-04-19 96 views
0

我有我的裝飾問題:爲什麼裝飾失敗?

class ReviewDecorator < Draper:Decorator 
    delegate_all 

    def author 
    @author = User.find_by(review.user_id) 
    "#{@author.firstname} #{@author.lastname}" 
    end 
end 

我每次測試這個裝飾,我收到此錯誤:

ReviewDecorator#author displays review author fullname Failure/Error: expect(review.author).to eq 'John Doe' NoMethodError: undefined method firstname' for nil:NilClass # ./app/decorators/review_decorator.rb:7:in author' # ./spec/decorators/review_decorator_spec.rb:10:in `block (3 levels) in '

Rspec的測試:

require 'spec_helper' 

describe ReviewDecorator do 

    let(:user) { build(:user, firstname: 'John', lastname: 'Doe') } 
    let(:review) { described_class.new(build(:review, user: user)) } 

    describe '#author' do 
    it 'displays review author fullname' do 
     expect(review.author).to eq 'John Doe' 
    end 
    end 
end 

我在做什麼錯誤?

+0

你能否發佈規範的相關部分呢?在調用此測試之前,您是否在測試數據庫中存在作者或存在作者? –

+0

我編輯了我的主帖並添加了rspec。 – Nekron

回答

1

根據你的測試文件,代碼應該是這樣的。

class ReviewDecorator < Draper:Decorator 
    delegate_all 

    def author 
    "#{user.firstname} #{user.lastname}" 
    end 
end