How to test more than one thing after lambda in rspec?

So, I have something similar in Rails with rspec:

it "should create a new user" do
  lambda do
    post :create, @attr
  end.should change(User,:count)
end

But post: create, @attr creates both the user and the company, so how do I “chain” change calls so that I can check both? What I'm looking for seems likeend.should change(User,:count) && change(Company,:count)

+5
source share
3 answers

I would say that you are trying to argue a lot in one test and do not match the name of the test. Instead, consider this:

it "should create a new user" do
  lambda do
    post :create, @attr
  end.should change(User,:count)
end

it "should create a new company" do
  lambda do
    post :create, @attr
  end.should change(Company,:count)
end

In addition, you may not know that it is better to write statements that do the same thing, but read much nicer:

expect {
  post :create, @attr
}.to change(Company, :count)
+8
source

, :

expect{ execute }.to change{ [spe1.reload.trashed?, spe2.reload.trashed?] }.from([true, true]).to([false, false])

:

[User, Company].each do |klass|
  it "creates one #{klass}" do
    expect{ post :create, valid_args }.to change(klass, :count).by(1)
  end
end
+1

@idlefingers - re:

"I would argue that you are trying to argue a lot in one test, and that doesn't match the name of the test."

  • Sometimes, however, I don’t need another separate example to just perform a different statement, for performance reasons, and shorten the test suite time.

To get around this, you can use this trick:

def user_and_company_count
  User.count + Company.count
end

it "can assert both counts" do
  expect { post :create, @attr }.to change(self, :article_an_activity_count).by(2)
end
0
source

All Articles