trouble testing file upload: undefined method `authenticate!' for nil:NilClass - ruby-on-rails

My app needs the user to upload a CSV and I need to test this functionality
and it uses an import route and saves that data in an instance variable
but in the process of writing this test I am getting this error
undefined method `authenticate!' for nil:NilClass
many others have encountered this error in the past, but apparently the previous fix of adding
RSpec.configure do |config|
config.include Devise::TestHelpers, type: :controller
end
no longer works for Devise 3, and I have been unable to find another work around
shotlist_presets_controller.rb
def import
if #spreadsheet = ShotlistPreset.open_spreadsheet(params[:file])
render :index
flash[:notice] = "Import Successful!"
else
flash[:notice] = "Import Failed!"
render :index
end
end
shotlist_presets_controller_spec.rb
describe ShotlistPresetsController do
include ActionDispatch::TestProcess
include RSpec::Rails::ControllerExampleGroup
before :each do
#preset_client = FactoryGirl.create(:client, :display_name => 'Nike')
#preset_user = FactoryGirl.create(:user, :first_name => "Stay", :last_name => "Lurkn", :client_id => #preset_client.id)
#preset_user.add_role(:manager)
visit shotlist_presets_path
end
before :each do
login_as(#preset_user)
visit shotlist_presets_path
end
it 'should upload a file' do
#file = fixture_file_upload("example.csv", 'text/csv')
post :import, :file => fixture_file_upload("example.csv", 'text/csv')
end
end
Any help in this matter would be greatly appreciated.

Related

Rspec not working properly: unable to find matching line

Im writing an rspec which does this
require 'rails_helper'
feature 'People can sign-in' do
include TestFactories
include Devise::TestHelpers
scenario 'Log in successfully' do
# request.env["HTTP_REFERER"] = '/'
visit root_path
#user = authenticated_user #defined in test_factories.rb
sign_in #user
sign_in #user
end
end
But when I try to run it I get the following error:
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `env' for nil:NilClass
I already tried to add:
request.env["HTTP_REFERER"] = '/'
But this doesn't help. Ant clues what I should do to get this working?
I just encountered the same error.
Here was my quick fix, tailor it to your needs.
post "/some/page", {:first_name => "john", :last_name => "doe"}, {:referer => '/some/page'}

Rspec controller post request to render different partials

I am trying to write a controller spec to test that the right partial is rendering after a post request.
Here is the controller method being posted to:
def lookup
#guest = Guest.where("mobile_number = ?", params[:lookup_mobile_phone_number]).first_or_initialize do |g|
g.mobile_number = params[:lookup_mobile_phone_number]
end
if #guest.new_record?
#visit = Visit.new(hotel_id: params[:hotel_id])
render partial: "guests/form"
else
#visit = Visit.new(guest_id: #guest.id, hotel_id: params[:hotel_id])
render partial: "visits/form"
end
end
Here is the spec/controllers/guests_controller_spec.rb I wrote that is failing:
RSpec.describe GuestsController, :type => :controller do
describe "#lookup" do
render_views
let!(:returning_guest) { create(:test_guest) }
context "when guest is already registered with hotel" do
it "renders visits/form" do
post :lookup, :guest => { :lookup_mobile_phone_number => "5553331212"}
expect(response).to render_template(:partial => 'visits/form')
end
end
end
end
Here is the factory I'm using for :test_guest
FactoryGirl.define do
factory :test_guest, :class => 'Guest' do
name 'Jack Guest'
mobile_number '5553331212'
end
end
This is the response I am getting when the test fails
1) GuestsController#lookup when guest is already registered with hotel renders visits/form
Failure/Error: expect(response).to render_template(:partial => 'visits/form')
expecting partial <visits/form> but action rendered <["shared/_hotel_agent_name", "_hotel_agent_name", "guests/_form", "_form"]>.
Expected {"shared/_hotel_agent_name"=>1, "_hotel_agent_name"=>1, "guests/_form"=>1, "_form"=>1} to include "visits/form".
# ./spec/controllers/guests_controller_spec.rb:16:in `block (4 levels) in <top (required)>'
I've been hacking away at this a for a few days now, trying different approaches found on here with no luck. Any help would be much appreciated :)
You send
post :lookup, :guest => { :lookup_mobile_phone_number => "5553331212"}
but in controller, you use
params[:lookup_mobile_phone_number]
not
params[:guest][:lookup_mobile_phone_number]
So to fix it, according to your controller, do
post :lookup, :lookup_mobile_phone_number => "5553331212"

Getting undefined method get on a named route in ruby on rails

I keep getting this error that I just can't figure out:
Failure/Error: get :find_movies, {:id => #id_passed }
NoMethodError:
undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x0000000363f800>
For the following rspec test:
it 'should find the movie in the database by the passed id' do
Movie.should_receive(:find).with(#id_passed).and_return(#movie_found)
get :find_movies, {:id => #id_passed }
end
which uses the following route:
get '/movies/find/:id' => 'movies#find', :as => :find_movies
And my controller includes:
def find
#movie = Movie.find params[:id]
if #movie.director != ""
#similar = Movie.where({:director => #movie.director}).all if #movie.director
else
flash[:warning] = "\'#{#movie.title}\' has no director info"
redirect_to movies_path
end
end
My friend pretty much wrote the exact same code and got it working. Can anyone help me figure out why this isn't working? Many thanks!
rspec may not be seem to take that your spec is a controller spec.You can add a type to your describe like this
describe YourController, :type => :controller do
...
end
Another fix may be by adding require 'rspec/rails' to spec_helper
If you are using 'spec/features', you may need to add the following to your 'spec_helper.rb'
config.include RSpec::Rails::RequestExampleGroup, type: :feature
See this answer for more : undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000106db51f8>

Undefined local variable or method `params` Rspec

I have the following controller RSpec test that should work however when I try to execute the below code:
require 'spec_helper'
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe UsersController do
include Devise::TestHelpers #Include devise test helpers
render_views # Render devise views
describe "GET 'show'"
before(:each) do
#user = User.find(params[:id])
#attr = {:initials => 'EU', :name => 'Example', :full_name => 'Example User',
:email => 'user#example.com', :password => 'password', :password_confirmation => 'password'
##attr = User.all
}
end
it 'should be successful when showing OWN details' do
get :show, :id => #attr
response.should be_success
end
it 'should find the correct user' do
get :show, :id => #attr
assigns(#attr).should == #attr
end
end
However I am getting the following output: Undefined locacl variable or method 'params' for RSPRC I believe this set up should be correct.
I see a few things going on here.
Your calling params within the before statement but where is the id ever set? No where in this code do I see where the user id is ever actually determined/found. You should set #user explicitly using either an id that you know exists or doing something such as User.first.
Then instead of calling
get :show, :id => #attr
you should be calling
get :show, :id => #user.id
Also I'm not sure why you need to include spec_helper twice. Line 2 should be able to be removed.
One more thing - assigns(#attr).should == #attr doesn't make any sense. It should be assigns(:attr).should == #attr. Otherwise you would be passing the value of #attr into the assigns method.

How to test Devise in a Controller RSpec, error: undefined method `assign' for #<RSpec::Core::ExampleGroup::Nested_1:0x0000010597f4b8>

I'm getting the following error:
undefined method `assign' for #<RSpec::Core::ExampleGroup::Nested_1:0x0000010597f4b8>
When attempting to test per the docs.
Here's is what I have:
user_controller_spec.rb
require 'spec_helper'
describe "devise/sessions/new.html.erb" do
let(:user) do
stub_model(User).as_new_record
end
before do
assign(:user, user)
# Devise provides resource and resource_name helpers and
# mappings so stub them here.
#view.stub(:resource).and_return(user)
#view.stub(:resource_name).and_return('user')
#view.stub(:devise_mapping).and_return(Devise.mappings[:user])
end
it "renders a form to sign the user in" do
render
rendered.should have_selector("form",
:method => "post",
:action => user_session_path
) do |form|
form.should have_selector("input", :type => "submit")
end
end
end
Suggestions? Thanks
Rspec has changed a bit, it's now using assigns.
Doc here.

Resources