Skip to content

Instantly share code, notes, and snippets.

@okitan
Created May 14, 2011 05:40
Show Gist options
  • Save okitan/971950 to your computer and use it in GitHub Desktop.
Save okitan/971950 to your computer and use it in GitHub Desktop.
こんな感じでAPIのspecを楽に書きたいんだけど
describe_jsonapi 'GET /users/:id' do
let(:id) { 1 }
context 'when user exists' do
before(:all) do
User.make(:id => id)
Article.make(:user_id => id, :article_id => 1)
end
after(:all) { DatabaseCleaner.clean }
it { status_code.should be_ok }
response_headers do
#its(['Contnet-Type']) { should =~ %r(^application/json) }
end
response_body do
its(:id) { should == 1 }
# its('id') { should == 1 } # also ok
# its([:id]) { should == 1 }
member :articles do
its(:size) { should == 1 }
element :first do
its(:article_id) { should == 1 }
end
end
end
end
context 'when user does not exist' do
it { status_code.should be_not_found }
response_headers { }
reponse_body do
member :errors do
element :first do
its(:message) { should == 'user not found' }
end
end
end
end
end
__EOF__
GET /users/:id
when user exists
status code should be 200 OK
in response headers
Content-Type should start with application/json # auto-gen by response_headers
in response body
member id should == 1
member articles
its size should == 1
should be an array # auto-gen by element
.first
article_id should == 1
when user does not exist
status code should be 404 Not Found
in response headers
Content-Type should start with application/json
in response body
member errors
should be an aray
.first
mesage should == 'user not found'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment