Skip to content
aripollak edited this page Nov 15, 2014 · 10 revisions

Check out these resources:

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.