How to test AJAX with Ruby on Rails? - ruby-on-rails

Can somebody give advice, how to test AJAX in Ruby on Rails?
For example: i click to button, query going to TestController#new, and in that method we have respond_with(#test) and new.js.erb, where is html form. Ok, i can click_to this link/button, xhr query will work, but i don't get any responses, which i need to test.
Can some body help with this, please?
I'm using Rspec.

In Rails 4.2 , for send ajax request, set optional hash argument with key "xhr: true" to call of get, post, patch, put, or delete method.
For example:
get users_path + '?page=2', xhr: true
and then use assert_select for verify some HTML elements on updated page.

You should use Capybara and Selenium. Look at this great Railscast.

Depending on what your Ajax request has .. it may actually be best to test it on multiple levels.
Test your controller's response with a controller test.
Test your JS correctly dealing with this response with a pure JS testing framework like jasmine.
Test your flow through the application with something like cucumber, or an integration rspec (with the help of capybara/selenium).

Related

Outputting HTTP request/response data with RSpec

I have a rails project that serves a JSON API with tests written in RSpec. Often when running specs (request specs, specifically), I’m interested in seeing some details about the HTTP request/response...i.e. the request URL, request body, and response body, ideally JSON pretty-formatted for readability. This isn't for the purposes of documentation but rather as part of the development / debugging process.
I have a helper method I wrote which does this...you just drop a method call into your spec and it prints this stuff out.
But, seems like it would be better if there was a switch that’s part of the running specs. RSpec has custom formatters which I thought might be the right direction, but in trying to build one, I can't figure out how to get access to the request/response objects like you can from inside of your spec.
How can I access the request/response objects in my custom RSpec formatter? Or, perhaps another way to approach the problem?
Here's an approach:
Assuming a rails project, in spec_helper.rb, define a global "after" hook like so:
config.after(:each) do #runs after each example
if ENV['PRINTHTTP']
#use request/response objects here, e.g. puts response.status
end
end
Then, you can conditionally enable by adding the environmental variable on the command-line:
$ PRINTHTTP=1 rspec

In a controller spec, how to find a specific tag in the generated view?

I know integration tests are preferred but I need this to be ran in a controller test, I'm testing a gem injecting html code in the view, especially with xhr so this can't be run in a feature spec (if it can, please explain me how :) )
So with rspec controller tests you can assert a selector is present (with capybara) :
response.body.should have_selector('#foobar')
has_selector? will call the all method from capybara to find the selector.
What I want to do is get the last child of body and then assert that its id is something in particular.
AFAIK it's not possible to do this with have_selector.
What I would do is :
all('body:first-child').first.id.should == '#foobar'
However, with Capybara DSL, all is defined like this (more or less):
def all(*args)
page.all(*args)
end
And the page will be empty unless I use visit but it's for integrations specs.
How can I use capybara all method inside an rspec controller test ?
I can't test it right now but after some googling it seems like this would do the trick
def page
Capybara::Node::Simple.new(response.body)
end
Source

How do I write a spec to verify the rendering of partials?

I'm using rr (the mocking framework) and rspec with ruby-on-rails. Also, I'm using the collection short hand for partial rendering. My question: How do I correctly fill out the the following view spec?
describe 'my_view' do
before(:each) do
assigns[:models] = Array.new(10, stub(Model))
end
it "should render the 'listing' partial for each model" do
# help me write something that actually verifies this
end
end
I've tried a few examples from the rspec book, rspec docs, and rr docs. Everything I try seems to leave me with runtime errors in the test - not failed assertions. Rather than show all the transformations I've tried, I figured all I'd need if someone showed me one that actually worked. I'd be good to go from there.
I would suggest asserting the presence of some HTML that the "listing" partial should generate. Otherwise, it sounds like you're trying to assert that Rails is technically calling render on the partial? That's the job of the Rails core tests to prove such functionality.

How should we test an rjs response in Rails Functional tests?

How can I test a .js.rjs response in rails(2.3.8) functional test ?
You can take the simple path and verify the contents being returned seem correct with a functional test.
However, you'll probably get a lot more value from something like Capybara and Celerity that will let you do real integration testing with a live JavaScript engine and verify the RJS causes the page behave you expect.
http://github.com/jnicklas/capybara/blob/master/README.rdoc
There's assert_select_rjs in case you weren't aware of it (like I was). Its something like an assert_tag for RJS output.

In rails, how do you stub the render method in functional tests?

I'm writing some functional tests for a controller in rails, using mocha to do mocking/stubbing.
Is there a way to prevent the template from being rendered during the test, so that I can test only the code in the controller?
It looks like rspec provides something like this, but I'm not using rspec.
The most obvious solution seems to work:
#controller.expects(:render)
I could have sworn that I tried that last night with no luck. But this morning it works like a charm. I must have overlooked a typo.
It doesn't look like using stub is necessary here. If you want to make sure that a given template is rendered, use assert_template and/or assert_response. You can also assert a state of the response object, either by hand or using helpers like assert_select.
Would render_to_string do what you need?

Resources