本文介绍了预期#count 已更改为 1,但未给出块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在测试我的 model 方法,该方法返回一个 Account 对象.我正在检查我的表是否插入了一个新的 row 并且我的模型反映了它的计数.

I am testing my model method which returns me an Account object. I am checking whether my table has inserted a new row and my Model reflects its count.

以下是我的规格.

 it "can create an account" do
    create_account = Account.create(account: acc)
    create_account.should change(Account, :count).by(1);
 end

我遇到的错误

8) Account can create an account
     Failure/Error: create_account.should change(Account, :count).by(1);
       expected #count to have changed by 1, but was not given a block

推荐答案

#change 匹配器需要一个块,其中执行的某些操作会影响预期的更改.试试这个:

The #change matcher expects a block in which some action is performed that effects the expected change. Try this:

expect { Account.create(account: acc) }.to change{ Account.count }.by(1)

参见 https://www.relishapp.com/rspec/rspec-expectations/v/2-0/docs/matchers/expect-change

这篇关于预期#count 已更改为 1,但未给出块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 09:51