预期#count已更改为1,但已更改为0

时间:2016-08-17 19:53:15

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

我在其中一个模型中进行了验证更改,这使我的测试失败了。我改变了我的一个工厂,希望能够修复我的测试。不幸的是,我的更改修复了所有这些,但其中一个,我收到错误,expected #count to have changed by 1, but was changed by 0

最终我正在处理一个表单并需要验证输入,所以在我的模型中我有。

validates :locations, presence: true

这最终导致测试失败:

   describe "#create" do
    it 'sends an invitation if invite=yes' do
      delivery_count = ActionMailer::Base.deliveries.size

      expect{
        binding.pry
        post :create, coach: FactoryGirl.attributes_for(:coach).merge(
          location: FactoryGirl.attributes_for(:location)
        ), invite: 'yes'
        binding.pry
      }.to change(Coach, :count).by(1)

      d = Coach.last
      expect(ActionMailer::Base.deliveries.size).to eq (delivery_count + 1)
      last_email = ActionMailer::Base.deliveries.last
      expect(last_email.to).to eq [d.email]
      expect(last_email.subject =~ /invitation instructions/).to be_truthy
    end

在我的工厂,我唯一改变的是将其从after移到before

  before :create do |coach|
    location = Location.first || create(:location)
    coach.locations << location
  end

在我有两个binding.pry的地方之间是计数应该添加的地方,并且我不确定我缺少什么来重新获得这个。如果有人可以看看,我肯定会很感激!

0 个答案:

没有答案