Skip to content
This repository

undefined method `env' for nil:NilClass in RSpec 2.10.1 #596

Closed
kwasimensah opened this Issue · 1 comment

2 participants

kwasimensah David Chelimsky
kwasimensah

This seems similar to issue #534 (#534) but the stack trace is different and upgrading to rspec-rails 2.10.1 didn't fix this.

  Failure/Error: request.env["CONTENT_TYPE"] = "application/json"
     NoMethodError:
       undefined method `env' for nil:NilClass
     # ./spec/requests/users_spec.rb:6:in `block (3 levels) in <top (required)>'
     # /Users/ananseAdmin/.rvm/gems/ruby-1.9.3-p194@helenadev/gems/rspec-core-2.10.1/lib/rspec/core/example.rb:87:in `instance_eval'
     # /Users/ananseAdmin/.rvm/gems/ruby-1.9.3-p194@helenadev/gems/rspec-core-2.10.1/lib/rspec/core/example.rb:87:in `block in run'
     # /Users/ananseAdmin/.rvm/gems/ruby-1.9.3-p194@helenadev/gems/rspec-core-2.10.1/lib/rspec/core/example.rb:195:in `with_around_each_hooks'
     # /Users/ananseAdmin/.rvm/gems/ruby-1.9.3-p194@helenadev/gems/rspec-core-2.10.1/lib/rspec/core/example.rb:84:in `run'
     # /Users/ananseAdmin/.rvm/gems/ruby-1.9.3-p194@helenadev/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:353:in `block in run_examples'
     # /Users/ananseAdmin/.rvm/gems/ruby-1.9.3-p194@helenadev/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:349:in `map'
     # /Users/ananseAdmin/.rvm/gems/ruby-1.9.3-p194@helenadev/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:349:in `run_examples'
     # /Users/ananseAdmin/.rvm/gems/ruby-1.9.3-p194@helenadev/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:335:in `run'
     # /Users/ananseAdmin/.rvm/gems/ruby-1.9.3-p194@helenadev/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:336:in `block in run'
     # /Users/ananseAdmin/.rvm/gems/ruby-1.9.3-p194@helenadev/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:336:in `map'
     # /Users/ananseAdmin/.rvm/gems/ruby-1.9.3-p194@helenadev/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:336:in `run'
     # /Users/ananseAdmin/.rvm/gems/ruby-1.9.3-p194@helenadev/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
     # /Users/ananseAdmin/.rvm/gems/ruby-1.9.3-p194@helenadev/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:28:in `map'
     # /Users/ananseAdmin/.rvm/gems/ruby-1.9.3-p194@helenadev/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:28:in `block in run'
     # /Users/ananseAdmin/.rvm/gems/ruby-1.9.3-p194@helenadev/gems/rspec-core-2.10.1/lib/rspec/core/reporter.rb:34:in `report'
     # /Users/ananseAdmin/.rvm/gems/ruby-1.9.3-p194@helenadev/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:25:in `run'
     # /Users/ananseAdmin/.rvm/gems/ruby-1.9.3-p194@helenadev/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:69:in `run'
     # /Users/ananseAdmin/.rvm/gems/ruby-1.9.3-p194@helenadev/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:10:in `block in autorun'

this is the content of the spec I was running.

require 'spec_helper'

describe "Users" do
  describe "signup new user"  do
    it "creates a new valid user" do
        request.env["CONTENT_TYPE"] = "application/json"
        post( users_path, { :format => :json, :user => {:email => "hello@gmail.com",
                                      :username => "testName",
                                      :password => "foobar",
                                      :password_confirmation => "foobar"}} )
        response.status.should be( 201 )
    end
  end
end
David Chelimsky
Owner

Issue #596 was about controller specs, not request specs. Different matter. The problem you're seeing is because Rails does not provide the request in an integration test (which is what lies beneath a request spec) until after the action.

What you want to do as add the CONTENT_TYPE to the the post:

post( users_path, { :format => :json, :user => {...} },
                  { "CONTENT_TYPE" => "application/json" } )

See http://rubydoc.info/gems/actionpack/ActionDispatch/Integration/RequestHelpers:post and http://rubydoc.info/gems/actionpack/ActionDispatch/Integration/RequestHelpers:get for more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.