我正在测试我的model
方法,该方法返回Account object
。我正在检查我的表是否插入了新的row
,我的模型反映了它的计数。
以下是我的规格。
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
答案 0 :(得分:44)
#change
匹配器需要一个块,在该块中执行一些影响预期更改的操作。试试这个:
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