Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert multiple change expectations within a single lambda request

Tags:

rspec2

I have a test like that:

lambda { post("/api/users", parameters) }.should change(User,:count).by(1)
lambda { post("/api/users", parameters) }.should_not change(ActionMailer::Base, :deliveries)

But I want to do it like that:

lambda { post("/api/users", parameters) }.should change(User,:count).by(1).and_not change(ActionMailer::Base, :deliveries)

Is it possible to do it without the need of two post calls?

Thanks

like image 651
Ricard Forniol Agustí Avatar asked Sep 28 '11 14:09

Ricard Forniol Agustí


2 Answers

I have found a solution to test it.

lambda{
  lambda { post("/api/users", params) }.should change(User,:count).by(1)
}.should change(ActionMailer::Base.deliveries, :count).by(1)
like image 62
Ricard Forniol Agustí Avatar answered Nov 06 '22 17:11

Ricard Forniol Agustí


In my tests I am very strict: I want each test to test only a single thing. So I would always choose the first form, not the second.

Secondly I am not sure it is technically possible. The .should expects a block, which is executed before and after the lambda. Anyway, to my knowledge currently rspec does not support this (and imho with good reason).

like image 2
nathanvda Avatar answered Nov 06 '22 18:11

nathanvda