Skip to content

Instantly share code, notes, and snippets.

@johndel
Created May 31, 2013 09:39
Show Gist options
  • Save johndel/5683931 to your computer and use it in GitHub Desktop.
Save johndel/5683931 to your computer and use it in GitHub Desktop.
Simple controller spec for testing json response.
require 'spec_helper'
describe Api::ElementsController do
let!(:user) { FactoryGirl.create(:user) }
let(:fire) { FactoryGirl.create(:element, name: "fire") }
let(:uber_fire) { FactoryGirl.create(:element, name: "uber fire") }
let(:vesuvius) { FactoryGirl.create(:element, name: "vesuvius", parents: [fire, uber_fire, fire]) }
let(:kolumbo) { FactoryGirl.create(:element, name: "kolumbo", parents: [fire, uber_fire, fire]) }
let(:response_body) { JSON.parse(response.body) }
describe "#get_known_elements" do
it "renders json false without user" do
post :get_known_elements
response.body.should == { success: false }.to_json
end
context "with user" do
before do
sign_in user
end
context "response json" do
it "includes success true" do
user.elements << vesuvius
post :get_known_elements
response_body["success"].should be_true
end
it "includes fire element" do
user.elements << vesuvius
post :get_known_elements
response_body["elements"].should include({ id: fire.md5_name, name: fire.name,
img_url: fire.image(:small) }.as_json)
end
it "includes vesuvius element" do
user.elements << vesuvius
post :get_known_elements
response_body["elements"].should include({ id: vesuvius.md5_name, name: vesuvius.name,
img_url: vesuvius.image(:small) }.as_json)
end
it "has not elements that not founded yet" do
vesuvius
post :get_known_elements
response_body["elements"].should_not include({ id: vesuvius.md5_name, name: vesuvius.name,
img_url: vesuvius.image(:small) }.as_json)
end
end
end
end
describe "#match_elements" do
before do
sign_in user
end
context "when not found element" do
it "renders json success false" do
post :match_elements, { elements: [fire.md5_name, fire.md5_name] }
response.body.should == { success: false }.to_json
end
end
context "when found element" do
it "renders json for success match element" do
vesuvius
kolumbo
post :match_elements, { elements: [fire.md5_name, uber_fire.md5_name, fire.md5_name] }
response.body.should == {
success: true,
elements: [{ id: vesuvius.md5_name, name: vesuvius.name, img_url: vesuvius.image(:small) },
{ id: kolumbo.md5_name, name: kolumbo.name, img_url: kolumbo.image(:small) }]
}.to_json
end
it "adds founded child element" do
vesuvius
kolumbo
post :match_elements, { elements: [fire.md5_name, uber_fire.md5_name, fire.md5_name] }
user.reload
user.elements.should include(vesuvius)
user.elements.should include(kolumbo)
end
it "should not add again allready founded element" do
user.elements << vesuvius
expect {
post :match_elements, { elements: [fire.md5_name, uber_fire.md5_name, fire.md5_name] }
}.to_not change { ElementUserMapping.count }
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment