nesquena / rabl

General ruby templating with json, bson, xml, plist and msgpack support
http://blog.codepath.com/2011/06/27/building-a-platform-api-on-rails/
MIT License
3.65k stars 335 forks source link

Rabl, Rspec, Spring #506

Open Govinda-Fichtner opened 10 years ago

Govinda-Fichtner commented 10 years ago

I am not really sure if this is the right place for my issue (maybe https://github.com/jonleighton/spring or rack-test is the correct one).

I tried to improve my testing experience by introducing spring into my test setup.

I have a controller rspec test that runs fine with `bundle exec rspec ...``

~/w/imageserver_xinet git:spring ❯❯❯ bundle exec rspec ./spec/controllers/api/v1/aspect_ratios_controller_spec.rb                                                                  
Run options: exclude {:slow=>true}

Api::V1::AspectRatiosController
  on GET to :index
    should respond with 200
    should respond with content type of application/json
    should assign @aspect_ratios
    responds with aspect ratios in json format

Finished in 0.1468 seconds
4 examples, 0 failures

while when I run it via spring I get the following

/w/imageserver_xinet git:spring ❯❯❯ spring rspec ./spec/controllers/api/v1/aspect_ratios_controller_spec.rb                                                                       
un options: exclude {:slow=>true}

Api::V1::AspectRatiosController
  on GET to :index
    example at ./spec/controllers/api/v1/aspect_ratios_controller_spec.rb:16 (FAILED - 1)
    example at ./spec/controllers/api/v1/aspect_ratios_controller_spec.rb:17 (FAILED - 2)
    example at ./spec/controllers/api/v1/aspect_ratios_controller_spec.rb:18 (FAILED - 3)
    responds with aspect ratios in json format (FAILED - 4)

Failures:

  1) Api::V1::AspectRatiosController on GET to :index
     Failure/Error: get :index, :format => :json
     ActionView::Template::Error:
       wrong number of arguments (0 for 1..2)
     # ./app/views/api/v1/aspect_ratios/index.json.rabl:2:in `_app_views_api_v__aspect_ratios_index_json_rabl___1930737646228633126_70195097420520'
     # ./spec/controllers/api/v1/aspect_ratios_controller_spec.rb:13:in `block (3 levels) in <top (required)>'

  2) Api::V1::AspectRatiosController on GET to :index
     Failure/Error: get :index, :format => :json
     ActionView::Template::Error:
       wrong number of arguments (0 for 1..2)
     # ./app/views/api/v1/aspect_ratios/index.json.rabl:2:in `_app_views_api_v__aspect_ratios_index_json_rabl___1930737646228633126_70195097420520'
     # ./spec/controllers/api/v1/aspect_ratios_controller_spec.rb:13:in `block (3 levels) in <top (required)>'

  3) Api::V1::AspectRatiosController on GET to :index
     Failure/Error: get :index, :format => :json
     ActionView::Template::Error:
       wrong number of arguments (0 for 1..2)
     # ./app/views/api/v1/aspect_ratios/index.json.rabl:2:in `_app_views_api_v__aspect_ratios_index_json_rabl___1930737646228633126_70195097420520'
     # ./spec/controllers/api/v1/aspect_ratios_controller_spec.rb:13:in `block (3 levels) in <top (required)>'

  4) Api::V1::AspectRatiosController on GET to :index responds with aspect ratios in json format
     Failure/Error: get :index, :format => :json
     ActionView::Template::Error:
       wrong number of arguments (0 for 1..2)
     # ./app/views/api/v1/aspect_ratios/index.json.rabl:2:in `_app_views_api_v__aspect_ratios_index_json_rabl___1930737646228633126_70195097420520'
     # ./spec/controllers/api/v1/aspect_ratios_controller_spec.rb:13:in `block (3 levels) in <top (required)>'

Finished in 0.12201 seconds
4 examples, 4 failures

Failed examples:

rspec ./spec/controllers/api/v1/aspect_ratios_controller_spec.rb:16 # Api::V1::AspectRatiosController on GET to :index
rspec ./spec/controllers/api/v1/aspect_ratios_controller_spec.rb:17 # Api::V1::AspectRatiosController on GET to :index
rspec ./spec/controllers/api/v1/aspect_ratios_controller_spec.rb:18 # Api::V1::AspectRatiosController on GET to :index
rspec ./spec/controllers/api/v1/aspect_ratios_controller_spec.rb:20 # Api::V1::AspectRatiosController on GET to :index responds with aspect ratios in json format

My rspec test looks like this

require 'spec_helper'

describe Api::V1::AspectRatiosController do
  describe "on GET to :index" do
    render_views

    let(:aspect_ratios) { [ mock_model(AspectRatio, :name => 'landscape'), mock_model(AspectRatio, :name => 'square') ] }

    before do
      AspectRatio.stub(:all).and_return(aspect_ratios)
      get :index, :format => :json
    end

    it { should respond_with :success }
    it { should respond_with_content_type :json }
    it { should assign_to :aspect_ratios }

    it "responds with aspect ratios in json format" do
      response = JSON.parse(@response.body)

      response.size.should eq 2
    end
  end

Basically the problem seems to be related to render_views, because if I remove the line and run the tests again with spring. I only have an error with the last test.

I tried to debug this and it throws an exception while initializing Rack::MockSession

From: /Users/govinda/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rack-test-0.6.2/lib/rack/mock_session.rb @ line 9 Rack::MockSession#initialize:

     7: def initialize(app, default_host = Rack::Test::DEFAULT_HOST)
     8:   @app = app
 =>  9:   @after_request = []
    10:   @default_host = default_host
    11:   @last_request = nil
    12:   @last_response = nil
    13: end

I am not really sure what the problem is and how to fix it. Has anybody seen something like this before?

I am using: Ruby 2.0.0p247 Rails 3.2.12 Rabl 0.9.0 rack-test 0.6.2 rspec 2.13.0 rspec-rails 2.13.2

dustineichler commented 9 years ago

I'm seeing this all over the place now too. Not sure if it's a respond_with block issue or something else. Rails can't digest the templates correctly without throwing an error now.