预期#count已经改变了1,但没有给出阻止

我正在测试我的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 

#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