cavalle / steak

DISCONTINUED - The delicious combination of RSpec and Capybara for Acceptance BDD
MIT License
763 stars 32 forks source link

Testing XHR and web services with Steak #26

Closed medihack closed 13 years ago

medihack commented 13 years ago

Hello Luismi, I am trying to also test XmlHttpRequest and web services with Steak. As Capybara (that could easily be integrated with Steak) doesn't handle that case (no xhr or post) method, I have to solve that problem somehow else. I read that Cucumber users do use methods from ActionDispatch::Integration::RequestHelpers (post, get, xhr, ...), but I can't get them to work outside my controller tests. Do you perhaps know how to get them in there? Maybe it would also be a nice feature to configure this by default.

medihack commented 13 years ago

I just solved the problem by including the below code in my acceptance helper. But maybe it would be a nice feature to have that in Steak directly. post and xhr methods are for example needed when doing integration tests of web services (at least I don't know another way to test that).

RSpec::Core::ExampleGroup.class_eval do
  include ActiveSupport::Concern
  include ActionDispatch::Integration::Runner
  include RSpec::Rails::BrowserSimulators

  def app
    ::Rails.application
  end

  def last_response
    response
  end
end

This code is derived from RSpec::Rails::RequestExampleGroup

medihack commented 13 years ago

Now I really wonder, because when I look at your specs of Steak (rspec-2/rails_spec.rb) you are using the get method. When I use the get method without that workaround above, I always get "undefined method `get' for #RSpec::Core::ExampleGroup::Nested_1:0xb65a83c0". So I just cloned your repo and ran your specs ... and they fail for the same reason:

1) Acceptance spec execution In order to write better web apps
  As a rails developer
  I want to execute acceptance specs Integration stuff
     Failure/Error: output.should =~ /1 example, 0 failures/
     expected: /1 example, 0 failures/,
          got: "F\n\nFailures:\n  1) Minimal spec First scenario\n     Failure/Error: get \"/\"\n     undefined method `get' for #\n     # /tmp/WkAeQRTiU/rails_app/spec/acceptance/QnsiGCppL_spec.rb:4\n\nFinished in 0.01108 seconds\n1 example, 1 failure\n" (using =~)
cavalle commented 13 years ago

Ok, there are two cases where get, post, etc. methods work out-of-the-box with Steak. The first one is with Rails2/Rspec1 where, for historical reasons, Rails integration test methods are available to make Webrat work. The second case is with Rails3/RSpec2 and Webrat, here Rails integration methods are not available but Webrat delegates in Rack::Test which provides the methods. I think both cases are tested in the specs.

With Capybara, however, while it also makes use of Rack::Test internally, it doesn't expose the methods through its own API (as Webrat does), and that's why you need to do something like what you mention in order to have the methods available in your specs (an alternative to your proposal would be to include Rack::Test or delegate to Capybara's rack_test driver)

Makes sense?

I see no compelling reason to add any specific support for these methods in Steak. If these methods work sometimes, it is just a side effect. It's not really acceptance testing what you're doing here (testing AJAX calls). For that reason I close this issue.

Cheers!

medihack commented 13 years ago

Hello Luismi,

thanks for the answer, it makes sense :-) I am just trying to figure out where the best place is to test my web services. Lets say I have a web service where a client can post some XML to create an object in the database. When I want to test that full functionality (using all layers) then I have to do some integrations tests. Ok, I admit, integration is not acceptance, but I have to test it somewhere. Maybe it's time to finally use that unused spec/requests folder :-)

cavalle commented 13 years ago

Good one. Testing an API is a good use case, one that makes me realize those those tests that work with webrat and rails2 were there for a reason. Making sure those methods are also available with capybara might make sense.

I reopen the issue, I'll try to write a patch later today.

medihack commented 13 years ago

Sounds good :-). One note ... it seems Rack::Test does not feature a xhr method. If you want to have that also in Steak, then I guess you have to include the ActionDispatch::Integration::Runner somewhere. Not sure if this would be a good idea.

cavalle commented 13 years ago

The good thing about Rack::Test is that it would work not only with Rails but also with any Rack compliant framework.

medihack commented 13 years ago

Good point. So I think Rack::Test will fit better, too. (I opened up my JSON anyway for non XHR requests.)

cavalle commented 13 years ago

Just pushed a new version of Steak (1.0.0.rc.4) which now generates an acceptance_helper that includes Rack::Test for capybara. get, post, etc. will be available, but not xhr. For that I'd suggest to write a request or controller spec.

For the next version (1.1) I'm considering to inherit from Rspec-Rails' RequestExampleGroup, which already takes care of including Webrat or Capybara. I've made this commit to explore this approach 47a415d4addbf54dce6a08eed09ae8ddf7aaedc0

medihack commented 13 years ago

Thank you, Luismi.