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

时间:2014-08-10 09:15:59

标签: ruby-on-rails ruby ruby-on-rails-3 rspec

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

1 个答案:

答案 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