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'}
Related
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.
I'm wrestling with Harlt's Chapter 9 exercise 9.
When I design my test for this exercise with :no_capybara set to true, like in other sections of the tutorial, the test PASSES, but I get the following warning:
WARNING: ignoring the provided expectation message argument (true) since it is not a string.
This version of the test is here:
*spec/requests/authentication_pages_spec.rb*
describe "as an admin user" do
let(:admin) {FactoryGirl.create(:admin)}
before do
sign_in(admin, :no_capybara => true)
end
describe "should not be able to delete itself by direclty submitting request" do
before { delete user_path(admin) }
specify { response.should redirect_to(users_path),
flash[:error].should =~ /Can't delete own admin account/i }
end
end
Note this this is how Hartl uses that method in other spaces of the tutorial, as follows:
*spec/requests/authentication_pages_spec.rb*
describe 'signed in user' do
let(:user) { FactoryGirl.create(:user) }
before { sign_in user, no_capybara: true }
describe 'unable to access now' do
before {get new_user_path}
specify { expect(response).to redirect_to(root_url)}
end
.
.
.ect...
However, when I do not set :no_capybara, my test fails:
describe "as an admin user" do
let(:admin) {FactoryGirl.create(:admin)}
before { sign_in(admin) }
.
.
.
Failures:
1) Authentication authorization as a non-admin user submitting a DELETE request to the Users#destroy action
Failure/Error: before { delete user_path(user)}
NoMethodError:
undefined method `admin?' for nil:NilClass
# ./app/controllers/users_controller.rb:68:in `admin_user'
# ./spec/requests/authentication_pages_spec.rb:74:in `block (5 levels) in <top (required)>'
My two main questions are:
Why is that warning occurring during that test, but not in other tests where I pass :no_capybara to the sign_in method?
Why is my test failing if I don't pass it no_capybara? I've seen the other questions on stackoverflow related to this exercise, and other people's solutions don't require no_capybara.
Below is all code within my app that might be applicable. If I should include more, please let me know.
*users_controller.rb*
before_action :admin_user, only: :destroy
def destroy
user = User.find(params[:id])
if !current_user?(user)
user.destroy
flash[:success] = "User destroyed. ID: #{user.id}"
else
flash[:error] = "Can't delete own admin account"
end
redirect_to users_path
end
def admin_user
redirect_to(root_url) unless current_user.admin?
end
*controllers/helpers/session_helper.rb*
def current_user
remember_token = User.encrypt(cookies[:remember_token])
#current_user ||= User.find_by(remember_token: remember_token)
end
I think in second case rails couldn't able to find admin? method since its not present.
Thats why you are getting this error
NoMethodError:
undefined method `admin?' for nil:NilClass
did you ran these codes
$ rails generate migration add_admin_to_users admin:boolean
$ bundle exec rake db:migrate
$ bundle exec rake test:prepare
As per your error, i think rails is looking for a method instead of just checking boolean value for admin.
The following rspec test file
require 'spec_helper'
describe EvaluationsController do
render_views
before(:each) do
association_attr
end
describe "'eval_selektor'" do
before(:each) do
#eval_selektor = get :eval_selektor, student_group_id: #student_group
end
it "should be successful" do
#eval_selektor
response.should be_success
end
...
end
...
end
is throwing the following error:
1) EvaluationsController 'eval_selektor' should be successful
Failure/Error: #eval_selektor = get :eval_selektor, student_group_id: #student_group
NoMethodError:
undefined method `student_groups' for nil:NilClass
# ./app/controllers/application_controller.rb:7:in `get_student_group'
# ./spec/controllers/evaluations_controller_spec.rb:14:in `block (3 levels) in <top (required)>'
from this method in the application_controller:
def get_student_group
#user = current_user
#student_group = #user.student_groups.find(params[:student_group_id])
end
At first I thought maybe rspec just wasn't getting passed the method from application_controller, but that's not the case as it can see it in the error. The code works in the browser, and if I put <%= #user %> in the view, it shows the correct user instance. Any ideas why rspec can't read #user?
apneadiving got me started in the right direction, and between this post and this one I got to the correct code:
ApplicationController.any_instance.stub(:current_user).and_return(#user)
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>
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.