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>
Related
I have set_seo method in my ApplicationController
def set_seo(page)
set_meta_tags :site => '', :title => page.seo_title ? page.seo_title : '', :reverse => true,
:description => page.seo_description ? page.seo_description : ""
end
and want to test my BrandsController which includes this method
def index
#some code here
set_seo(#content)
end
my controller spec
require 'spec_helper'
describe BrandsController do
render_views
before(:all) do
#content = create(:content)
create(:user)
end
describe "GET index" do
it "index" do
allow(ApplicationController).to receive(set_seo(#content)).and_return(set_seco(#content))
brand = Brand.create
get :index
expect(response).to render_template(:index)
end
but i have got
NoMethodError:
undefined method `set_seo' for #<RSpec::ExampleGroups::BrandsController::GETIndex:0x007f866daa96c8>
set_seo isn't defined in your specs, hence the undefined method error. You have to pass methods as symbols for mocks/stubs (In other words you're replacing or "mocking" the behavior of the method, not calling it directly).
That said, what's the point of this line: allow(ApplicationController).to receive(set_seo(#content)).and_return(set_seco(#content))? Isn't it telling :set_seo to return what it already returns?
If you're trying to ensure that set_seo is actually called, use the following:
expect_any_instance_of(ApplicationController).to receive(:set_seo).with(#content).and_call_original
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'}
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 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.
I'm trying to mimic something similar to http://relishapp.com/rspec/rspec-rails/v/2-2/dir/routing-specs/access-to-named-routes-in-routing-specs
I have a unit test passing:
require 'test_helper'
class RoutesTest < ActionController::TestCase
test "book name is sent to store#index' do
assert_routing 'book/mytitle', {:controller => 'book', :action => 'index', :title => 'mytitle'}
end
end
I'm trying to covert this to an RSpec test (running Rspec 2.2 under Rails3.0.3)
Here's the test:
require 'spec_helper'
include RSpec::Rails::Matchers::RoutingMatchers
include ActionDispatch::Assertions::RoutingAssertions
describe "book specific routes" do
it "should recognize title in path" do
{:get => "book/mytitle"}.should route_to(:controller => "book", :action => "index", :title => "mytitle")
end
end
But this results in:
Failures:
1) book specific routes should recognize title in path
Failure/Error: {:get => "book/mytitle"}.should route_to(:controller => "book", :action => "index", :title => "mytitle")
undefined method `recognize_path' for nil:NilClass
# ./spec/route_spec.rb:9:in `block (2 levels) in <top (required)>'
Any idea where the nilClass is coming from? Any help would be appreciated.
It's the double include of ActionDispatch::Assertions::RoutingAssertions which causes the failure -- not sure why. Remove the two include statements and all should be well. The spec file should live in /spec/routing. You can wrap the example with describe BooksController for style points, but it will work without it.
I'm guessing the matchers need to be used inside a controller spec. They should already be there, so there would be no need to include them manually. Just make sure you're describing a controller.
describe BooksController do
it "should recognize title in path" do
# ...
end
end