Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expected #count to have changed by 1, but was not given a block

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.

Below is my spec.

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

Error i am getting

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
like image 769
UnderTaker Avatar asked Aug 10 '14 09:08

UnderTaker


People also ask

What u mean by expected?

to look forward to; regard as likely to happen; anticipate the occurrence or the coming of: I expect to read it. I expect him later. She expects that they will come. to look for with reason or justification:We expect obedience. Informal. to suppose or surmise; guess: I expect that you are tired from the trip.

What is the synonym of expected?

Synonyms for expected. anticipated, awaited, hoped (for), watched (for)

Has expected meaning?

1. to regard as probable or likely; anticipate: he expects to win. 2. to look forward to or be waiting for: we expect good news today. 3. to decide that (something) is requisite or necessary; require: the boss expects us to work late today.

Is expect or expected?

The difference is simply that What do you expect to know? poses a question in the "active" voice (for which the answer might be I expect to know X). By contrast, What are you expected to know? is "passive" (a typical answer being I am expected to know X).


1 Answers

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)

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

like image 143
Thilo Avatar answered Nov 13 '22 22:11

Thilo