Rspec: expect(response).to have one of several valid values

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Rspec: expect(response).to have one of several valid values

Ralph Shnelvar
I'm a novice at Rspec.


Let's say I have
expect(response).to have_http_status(200)

But what I want is to say that either 200 or 302 is valid.

How would I do that?


Assume
(byebug) response.status
302

I've tried
expect(response).to (have_http_status(200) || have_http_status(302))

While syntactically valid, this doesn't work because have_http_status(200)will throw an exception before it gets to the have_http_status(302) .


Is this the right forum to ask this question?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/11fe797f-b3d2-484b-bdc3-ebf3410edbe9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rspec: expect(response).to have one of several valid values

Dave Aronson
On Saturday, July 15, 2017, Ralph Shnelvar <[hidden email]> wrote:

I've tried
expect(response).to (have_http_status(200) || have_http_status(302))

While syntactically valid, this doesn't work because have_http_status(200)will throw an exception before it gets to the have_http_status(302) .

Maybe something like "expect([200, 302]).to include response.status_code"?  (Not sure of exact syntax as I've been away from RSpec for a while and am not at my coding computer.)


--
Sent from Gmail Mobile; please excuse top posting, typos, etc. :-(

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAHxKQigAzomwfbVea6JRKACDJgxXs0ozxd8W76%3D9%2BkcG4fO2Mg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rspec: expect(response).to have one of several valid values

Ralph Shnelvar
Very close!

expect([200, 302]).to include response.status

Thank you so much!  Your suggestion gave me a whole new understanding of Rspec

Ralph


On Saturday, July 15, 2017 at 7:48:49 PM UTC-6, Dave Aronson wrote:
On Saturday, July 15, 2017, Ralph Shnelvar <[hidden email]> wrote:

I've tried
expect(response).to (have_http_status(200) || have_http_status(302))

While syntactically valid, this doesn't work because have_http_status(200)will throw an exception before it gets to the have_http_status(302) .

Maybe something like "expect([200, 302]).to include response.status_code"?  (Not sure of exact syntax as I've been away from RSpec for a while and am not at my coding computer.)


--
Sent from Gmail Mobile; please excuse top posting, typos, etc. :-(

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/62013409-cdce-4088-a4f0-bba5d2b51406%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rspec: expect(response).to have one of several valid values

Frederick Cheung-2
In reply to this post by Ralph Shnelvar


On Sunday, July 16, 2017 at 1:43:01 AM UTC+1, Ralph Shnelvar wrote:

expect(response).to (have_http_status(200) || have_http_status(302))

While syntactically valid, this doesn't work because have_http_status(200)will throw an exception before it gets to the have_http_status(302) .

This doesn't work, but not for the reason you think. Calling have_http_status(200) never raises an exception. It creates a matcher object that the `to` method uses. The ruby || operator (one of few things that can't be overridden) evaluates to the first non falsy thing, so your code is the same as 

expect(response).to (have_http_status(200) || have_http_status(302))

Rspec (since 3.0) does however have compound / composable matchers (see https://relishapp.com/rspec/rspec-expectations/v/3-6/docs/compound-expectations, http://rspec.info/blog/2014/01/new-in-rspec-3-composable-matchers/)

so expect(response).to have_http_status(200) | have_http_status(302)

should work, as would things like

expect(3).to eq(2) | eq(3)

This works because | and & are overridable methods and have been defined on matchers to mean "create me a new matcher which matches if either argument matches" (or both match in the & case)

Fred

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/8ef74046-a257-43cb-b2d8-dc95fa46d314%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...