0

デコレータに問題があります:

class ReviewDecorator < Draper:Decorator
  delegate_all

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

このデコレーターをテストするたびに、次のエラーが表示されます。

ReviewDecorator#author はレビュー著者の氏名を表示します Failure/Error: expect(review.author).to eq 'John Doe' NoMethodError: undefined method firstname' for nil:NilClass # ./app/decorators/review_decorator.rb:7:inauthor' # ./spec/decorators/review_decorator_spec.rb:10:in `block (3 レベル)の '

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

私が間違っていることは何ですか?

4

1 に答える 1

1

テストファイルによると、コードは次のようになります。

class ReviewDecorator < Draper:Decorator
  delegate_all

  def author
    "#{user.firstname} #{user.lastname}"
  end
end
于 2015-04-19T12:42:43.807 に答える