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
Related
This is my Spec file:
require 'rails_helper'
RSpec.describe Programmes::ReportsController, :type => :controller do
let!(:programme) { create(:programme) }
context 'authenticated user' do
describe 'GET index' do
it 'responds with a 200 OK status code' do
get :index, params: { id: programme.id }
expect(response).to have_http_status(:success)
end
end
end
end
This is my Factory;
FactoryGirl.define do
factory :programme do
name { Faker::Lorem.word }
description { Faker::Lorem.sentence(3) }
end
end
This is my Controller;
class Programmes::ReportsController < ApplicationController
def index
end
def create
end
end
I can't seem to get this spec to pass. The route works fine in the browser; eg
http://localhost:3000/programmes/{:id}/reports
The error I have is:
Failures:
1) Programmes::ReportsController authenticated user GET index responds with a 200 OK status code
Failure/Error: let!(:programme) { create(:programme) }
NoMethodError:
undefined method `create' for #<RSpec::ExampleGroups::ProgrammesReportsController::AuthenticatedUser::GETIndex:0x007fac78b1b440>
# /Users/mike/.rvm/gems/ruby-2.2.3/gems/actionpack-5.0.0/lib/action_dispatch/testing/assertions/routing.rb:172:in `method_missing'
I am quite new to Ruby (and Rails). I don't think the Programme object is being created in FactoryGirl - but I don't really know how to find out if that's the case
Did you require 'factory_girl' in spec_helper?
There is the following spec:
describe 'Some title' do
before do
session[:state] = "12334"
get '/api/v1/menus', format: :json
end
it 'some text' do
expect(response).to be_success
json = JSON.parse(response.body)
puts json
end
end
It code tests the following controller's action:
class Api::V1::MenusController < ActionController
def index
render json: session
end
end
But I've got the following exception: "undefined method `session' for nil:NilClass". How can I fix it? How can I make a new example of a session? Thanks in advance.
Try this:
describe 'Some title', :type => :controller do
RSpec needs to know you are doing "controller things" in your test. You indicate this as above or by placing the test in spec/controllers.
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.
I have a Rails 3 project in which I want to store the current company selected in a session variable.
I'm working with the staff controller spec and would like to stub out current_company for now as I'm isolating my spec example for the staff new controller action.
it "should call current_company" do
company = mock_model(Company, :id => "1")
controller.should_receive(:current_company).and_return(company)
get :new
end
Here is my new action for the staff controller
def new
#staff = Staff.new
#staff.company_id = current_company.id
end
I keep getting error
Failure/Error: get :new
NameError:
undefined local variable or method `current_company' for #<StaffsController:0x000000028d6ad8>
I've also tried just stubbing it out instead of using should_receive
controller.stub!(:current_company).and_return(company)
I get the same error.
Your code looks fine to me, it should work. There must be some other problem we are not seeing. I notice the controller name is "StaffsController" -- is that correct? Double-check the names of the controller and the corresponding spec -- they should be the same.
I think it was bombing out on the 'should be successful' example/test, so I've put my stubbing in a before block.
require 'spec_helper'
describe StaffsController do
describe "GET 'new'" do
let(:staff) { mock_model(Staff, :company_id= => nil)}
let(:company) { mock_model(Company, :id => 1)}
before do
Staff.stub!(:new).and_return(staff)
controller.stub!(:current_company).and_return(company)
end
it "should be successful" do
get :new
response.should be_success
end
it "should call current_company" do
controller.should_receive(:current_company).and_return(company)
get :new
end
end
end
This works for:
class StaffsController < ApplicationController
def new
#staff = Staff.new
current_company.id
end
end