-
Notifications
You must be signed in to change notification settings - Fork 333
Testing with rspec
aripollak edited this page Nov 15, 2014
·
10 revisions
Check out these resources:
- https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec
- http://stackoverflow.com/questions/9576756/using-rspec-how-do-i-test-the-json-format-of-my-controller-in-rails-3-0-11
- https://github.com/nesquena/rabl/issues/130#issuecomment-5927085
In a nutshell if you use controller specs be sure to use render_views if you need to test the rendered output:
describe ApplicationsController do
render_views
let(:model) { Factory.create(:model) }
subject { model }
context "JSON" do
describe "creating a new model" do
context "when not authorized" do
it "should not allow creation of an application" do
json = { :format => 'json', :model => { :name => "foo", :description => "bar" } }
post :create, json
Application.count.should == 0
response.status.should eq(401)
JSON.parse(response.body)["status"].should == "error"
JSON.parse(response.body)["message"].should =~ /authorized/
end
end
end
end
end
This advice might only pertain to Rails. In Sinatra or Padrino, my understanding is that this wouldn't be necessary.