Reputation:
I am trying write a simple Rspec for a serializer that I have in my rails application. I tried using:
ActiveModelSerializer::Matchers. Since this did not work. I did try to make my Rspec test for this look like the following:
require 'spec_helper'
describe CoffeeTypeSerializer do
it { should have_key(:name) }
it { should have_key(:image) }
it { should have_key(:type_of_coffee) }
it { should have_key(:temp) }
it { should have_key(:caffeinated) }
it { should have_key(:basicInfo) }
it { should have_key(:price) }
it { should have_key(:created_at) }
it { should have_key(:updated_at) }
it { should have_key(:fullInfo) }
it { should have_key(:drinks) }
end
Unfortunately had no luck with this and got the following error:
ArgumentError: wrong number of arguments (0 for 1..2)
Tried searching high and low, but what is the best way to test JSON serializers
Upvotes: 0
Views: 1464
Reputation: 43825
We've gone and basically rolled our own matchers because the JSON serializers weren't working for us either. They're a bit involved, so I've added them to a gist: https://gist.github.com/gavingmiller/e03ff13edfeef8d5c08d
The example tests end up looking like this:
require 'spec_helper'
describe FeedSerializer, type: :serializer do
it { should have_attribute(:id) }
it { should have_attribute(:name) }
it { should have_many_relation(:feed_filters) }
end
When we make modifications to the serializers (ie: different output than just straight attributes) our tests look like this:
it "successfully parses JSON as an additional field" do
@data = '{ "key": "value" }'
json = FooBarSerializer.new(data).as_json
json[:foo][:bar]['key'].should == 'value'
end
Upvotes: 2