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?
source share