-
Notifications
You must be signed in to change notification settings - Fork 335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Any thoughts about RSpec'ing views? #130
Comments
The way I do controller testing is pretty straightforward, I perform a request and then parse the json into a hash. I then test equality and/or patterns for different properties of the hash response. I verify the keys that are being returned, verify key values are correct, items are created as described in the response, status codes are correct, etc. It looks something like this: describe Foo do
before do
# Helper I created that does a "get" and makes a response hash available
json_get :index, :foo => "bar"
end
it "returns the expected keys" do
assert_equal ["key1", "key2"], @json_response['foo'].keys
end
it "sets correct value for foo" do
assert_equal "bar", @json_response['foo']['key1']
end
end That type of thing. Hope that's helpful. |
Thanks a lot, that was really helpful! |
Hi, it "should render foo"
get :index
content = JSON.parse(response.body)
content.should == "something"
end but now using rabl and checking response.body is always an empty string " ". Running the server and checking with curl or browsers renders the proper json. How does your json_get method look like? |
I have a Rails 2.3 project and use test/unit shoulda and the json_get method really just looks something like this (ok its more complicated, but this is the relevant part): def json_get(action, params)
get action
@json_response = JSON.parse(response.body)
end I have never had a problem with RABL returning an empty response body on Rails 2,3 or Padrino. Are you sure that the response body is always blank and your action isn't somehow getting caught by a before filter or something in your tests. |
Its a single test with a before block which creates objects and signs in the users. |
The problem is with Rspec and RABL. I created an empty applications github and wrote three tests: rspec controller test, rspec view test and test/unit controller test. rspec controller test fails ( |
Hmm, I don't use rspec in any of my RABL projects, I generally use minitest, riot or shoulda. If anyone has any insights into why this fails with rspec only as far as I can tell, I would happily accept a patch. I recognize that RABL should work with Rspec tests, but I don't know enough about it to fix the issue. |
Controller specs by default are run in isolation; see http://rspec.info/rails/writing/controllers.html This means the spec won't render any templates, but returns the result of @post.to_json. Because of that your specs started failing. The solution to testable rabl-output in your controller specs is to call describe Foo do
integrate_views
before do
# Helper I created that does a "get" and makes a response hash available
json_get :index, :foo => "bar"
end
it "returns the expected keys" do
assert_equal ["key1", "key2"], @json_response['foo'].keys
end
it "sets correct value for foo" do
assert_equal "bar", @json_response['foo']['key1']
end
end But beware that this will also render other templates (like erb); so be careful when adding. |
Great, thanks for explaining that. Can someone confirm that this solves the issue? |
I haven't thought about |
I can see why they would isolate the controller tests from the views. Is there a more appropriate place to test rabl in rspec then? I mean how would you test a builder template or any other equivalent type response? What's the appropriate place for that? View tests? or Integration tests? |
I don't actually know the best way for myself; I just got started playin' with rabl. The apps I worked on all used #to_xml and friends. I always wrote kind of view spec for them on the model-level; which includes setting up fixtures to serialize. With rapl it would maybe make sense to test them with mocks/stubs like you would test an plain old html view. But I never really got used to use view testing consequently; maybe I'll give it another try with rabl. I don't think it would make sense to test rabls output extensivly in full integration tests; I guess this would get really slow in no time. |
I'd prefer to isolate my rabl responses by testing only the rabl files themselves--not the controllers. I'm trying to figure out how to do this in rspec right now.... I'd be okay with throwing a real ActiveRecord object at rabl for my tests just to see if it's generating the JSON I expect. Any guidance would be awesome...! I'm not sure if rspec view specs are appropriate here, but they seem to be the most likely candidate. EDIT: It should rely on Rails still, and load all of the models and activerecord junk. EDIT: I figured it out.
If you're using the watchr gem (I use it in conjunction with spork), you'll want to modify your watchr config to include .rabl files, like so:
|
In an amendment to my previous post, I figured out how to test rabl templates without Rails.
Super fast specs! EDIT: took out a bit of useless code from one of my experiments. |
Very cool, isolated rabl view specs. Thanks for sharing. |
Also for others viewing this, be sure to check out http://stackoverflow.com/questions/9576756/using-rspec-how-do-i-test-the-json-format-of-my-controller-in-rails-3-0-11 as an alternative by using render_views. |
To information with the Rabl.render method it's now simplest. I do in my spec_view ( with using json_spec gem ) require 'spec_helper'
describe 'harbors/index.rabl' do
let(:harbor) { Fabricate(:harbor) }
let(:harbor_json) {
{
name: harbor.name,
lat: harbor.lat,
lng: harbor.lng
}.to_json
}
before do
@rendered = Rabl.render([harbor],
'harbors/index',
:view_path => 'app/views')
end
it 'should have one items' do
@rendered.should have_json_size(1)
end
it 'should have name information about harbor' do
@rendered.should include_json(harbor_json)
end
end |
great I also had the 2 empty response issue, now using render_views with rspec solves the problem |
Hello everybody,
I'd like to know about some
best practices
in testing Rabl–views.Thanks a lot.
The text was updated successfully, but these errors were encountered: