我的装饰师有问题:
class ReviewDecorator < Draper:Decorator
delegate_all
def author
@author = User.find_by(review.user_id)
"#{@author.firstname} #{@author.lastname}"
end
end
每次我测试这个装饰器时,都会收到此错误:
ReviewDecorator#author显示评论作者全名 失败/错误:期待(review.author)。到eq&#39; John Doe&#39; NoMethodError: 未定义的方法
firstname' for nil:NilClass # ./app/decorators/review_decorator.rb:7:in
作者&#39; #./spec/decorators/review_decorator_spec.rb:10:in块中的“块(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
我做错了什么?
答案 0 :(得分:1)
根据您的测试文件,代码应该是这样的。
class ReviewDecorator < Draper:Decorator
delegate_all
def author
"#{user.firstname} #{user.lastname}"
end
end