undefined方法`valid?`for“possimus”:String

时间:2016-10-21 11:34:21

标签: ruby-on-rails ruby-on-rails-4 rspec factory-bot

经过测试的代码:

context 'validations' do
let(:super_campaign){FactoryGirl.create(:super_campaign)}
   context 'package' do
    it "Package should be present" do
     expect(super_campaign.package).to be_valid
    end
  end
end

我收到的错误如undefined method "valid?" for "possimus":String

2 个答案:

答案 0 :(得分:2)

expect(super_campaign).to be_valid # AR object, not it's attribute

答案 1 :(得分:1)

您只能测试整个模型是否有效。但是,您可以测试package属性是否有错误。

context 'validations' do
let(:super_campaign){FactoryGirl.create(:super_campaign)}
   context 'package' do
    it "Package should be present" do
     expect(super_campaign).to be_valid
     expect(super_campaign.errors[:package]).to be_nil
    end
  end
end