How to use RSpec with JBuilder?

I am looking for a clean way to use JBuilder and check json output using RSpec. A popular way to test JSON is to implement the as_json method, and then in RSpec compare the resulting object with the object.to_json method. But the big reason I use JBuilder is because I don't want all the attributes that to_json spits out; therefore this violates the comparison.

Currently with JBuilder I need to do the following to check RSpec results:

1) Create a Factory object: @venue

2) Create a hash inside my RSpec test that contains the β€œexpected” JSON string back

@expected => {:id => @venue.id,:name=>@venue.name..........} 

2) Compare the string @expected with result.response.body, which is returned from the JSON call.

This seems simple, except that I have objects that display with attributes 15+, and building a string with a hash string forecast is cumbersome and very fragile. Is there a better way to do this?

+6
source share
4 answers

You should be able to test your Jbuilder views with RSpec view options. Documents can be seen at https://www.relishapp.com/rspec/rspec-rails/v/2-13/docs/view-specs/view-spec .

The spec example for the file located in 'app / views / api / users / _user.json.jbuilder' might be something like this (spec / views / api / users / _user.json.jbuilder_spec.rb):

 require 'spec_helper' describe 'user rendering' do let(:current_user) { User.new(id: 1, email: ' foo@bar.com ') } before do view.stub(:current_user).and_return(current_user) end it 'does something' do render 'api/users/user', user: current_user expect(rendered).to match(' foo@bar.com ') end end 
+4
source

This seems like a good use case for RSpec specifications. Are you using JBuilder to display the controller in the views?

For example, in spec / views / venues_spec.rb

 require 'spec_helper' describe "venues/show" do it "renders venue json" do venue = FactoryGirl.create(:venue) assign(:venue, venue]) render expect(view).to render_template(:partial => "_venue") venue_hash = JSON.parse(rendered) venue_hash['id'].should == @venue.id end end 
+1
source

I have not yet been able to get RSpec to work with views, but I am testing my JSON API through the tests of the RSpec controller. To help in this process, I use api matchers gem . This gem allows you to create RSpec tests, such as:

 it "should have a 200 code" do get :list, :format => :json response.should be_success response.body.should have_json_node( :code ).with( "200" ) response.body.should have_json_node( :msg ).with( "parameter missing" ) end 
+1
source

I do not like to test the JSON API through the views, because you should essentially simulate in the test the setup already done in the controller. Also, bypassing the controller, you are not actually testing the API.

However, in controller tests, you will find that no JSON is received in the response body. The response body is empty. This is due to the fact that RSpec disables view visualization in controller tests. (For better or for worse.)

To test the RSView controller of your rendered JSON API, you must add the render_views directive at the top of the test. See this blog post (not mine) for more information on using RSpec controller tests with Jbuilder.

Also see this answer .

+1
source

Source: https://habr.com/ru/post/912061/


All Articles