我正在爲類項目構建一個reddit克隆,並且我的答案模型始終未通過測試。爲答案保留失敗測試
answer_spec.rb:
require 'rails_helper'
RSpec.describe Answer, type: :model do
let(:question) { Question.create!(title: "New Question Title", body: "New Question Body", resolved: false) }
let(:answer) { Answer.create!(body: "New Answer Body", question: question) }
describe "attributes" do
it "has a body attribute" do
expect(Answer).to have_attributes(body: "New Answer Body")
end
end
end
,我發現了導致故障的,當我運行此:
Failures:
1) Answer attributes has a body attribute
Failure/Error: expect(Answer).to have_attributes(body: "New Answer Body")
expected Answer(id: integer, body: text, questions_id: integer, created_at: datetime, updated_at: datetime) to respond to :body with 0 arguments
# ./spec/models/answer_spec.rb:9:in `block (3 levels) in <top (required)>'
Finished in 0.02163 seconds (files took 1.94 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/models/answer_spec.rb:8 # Answer attributes has a body attribute
可能有人好心幫我嗎?
謝謝。
編輯
道歉不包括回答類
class Answer < ApplicationRecord
belongs_to :question
end
沒有看到你的答案類的代碼,沒有太多的幫助可以給。但是測試結果指出了這個問題,你的Answer類對「body」沒有響應,這意味着它沒有名爲body的屬性。 – Kyle
'expect(answer)'。 –