So I ran into this problem with Testing our API.

The problem is the get request is called multiple times based on examples. 
e.g this code below will run get 'test' twice.

require 'rails_helper'

describe API::TestController, type: controller do
  before do
     get 'test'
  end

  it { expect(response).to be_ok }
  it { expect(response.body).to eq('test code')end

This is a problem when you start to have more expect statements in terms of 
performance. As far as I know there is no good workarounds for examples to 
re use the same response. The guide herehttp://betterspecs.org/#single talks 
about putting multiple expects into the it statement, this seems to go 
against getting good failure responses.

Using a before(:all) you get an error like so

Failure/Error: get 'test'
     RuntimeError:
       @routes is nil: make sure you set it in your tests setup method.

Is there a way to send only one request without ruining the failure 
responses?
(or if you like use memoization over multiple examples)

I did find you could use a global variable but this seems like the worst 
code ever.

require 'rails_helper'

describe API::TestController, type: controller do
  it 'makes a single request' do
    get 'test'
    $stupid_global = response
  end
  it { expect($stupid_global).to be_ok }
  it { expect($stupid_global.body).to eq('test code')end


I posted this here https://github.com/rspec/rspec-core/issues/1876 and got 
this response:

This conundrum (shared state vs performance is one of the reasons we added 
compound matchers to RSpec 3.2, so you can now do:


it { expect(response).to be_ok.and eq 'test code' }


This isn't a complete solution of course but we don't want to advocate 
shared state across examples.

Incidentally Github issues are not the place to request support, please use 
the mailing list / google group (https:
//groups.google.com/forum/#!forum/rspec) and/or #rspec on freenode."


I really don't see this as a even usable solution as if you have 100 
expectations


And you compound those you end up with failure in one string like so:


Failure/Error: "we expected it to have this and  and we expected it to have 
this and we expected it to have this and we expected it to have this and we 
expected it to have this and we expected it to have this we expected it to 
have this we expected it to have this we expected it to have this we 
expected it to have this we expected it to have this we expected it to have 
this"

you don't compound them have one useless string with lots of expectations 

Failure/Error: "we expected the response to be ok (not sure why its not)"

or you make 100 requests (massive performance load).

Does anyone have any suggestions for better ways? Alternative testing 
frameworks? (maybe rspec just isn't useful for this kind of testing) or 
even a feature for shared state? (By the sounds of it this will not be 
supported)

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/fee46181-7a72-4e33-8f7e-1661aa30cba5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to