Undefined method 'action' running single RSpec file for a rails controller - ruby-on-rails

I have a peculiar situation - an rspec file fails when run independently, but run okay when run as part of the entire suite.
Failure/Error: visit oauth_callback_path
NoMethodError:
undefined method `action' for MyController:Class
# <internal:prelude>:10:in `synchronize'
# ./spec/requests/login_spec.rb:xx:in `block (5 levels) in <top (required)>'
# ./spec/requests/login_spec.rb:xx:in `block (4 levels) in <top (required)>'
Simplified spec:
require 'spec_helper'
class MyController
def oauth_response
sign_in(
ENV['TEST_ACCESS_TOKEN'],
ENV['TEST_ACCESS_SECRET'])
redirect_to root_path
end
end
describe 'logging in' do
it 'login' do
visit oauth_callback_path
response.should be_success
end
end

I believe the problem is that MyController is not extending ApplicationController. That's why the method action is not defined for MyController.

The class MyController appears to be blocking Rails magic class loading. Either the test should explicitly require the controller, or the extension should be defined with MyController.class_eval

Related

How to include view helper in RSpec

I have a module with a custom view helper. Actually it uses the modified code of link_to helper. Just append a query string to the generated link. I have existing tests which fails because the custom helper cannot be found. How can I include it so it is available for RSpec? I tried the following approaches:
1.Using include:
describe MyClass do
include MyHelper
2.Using configure
RSpec.configure do |config|
config.include MyHelper
end
3.Adding it in spec_helper.rb
config.include MyHelper
Here is the error:
1) Mailer#calendar_item_notification should send email for task
Failure/Error: expect {Mailer.calendar_item_notification('User', #user.id).deliver}.to change { ActionMailer::Base.deliveries.count }.by(1)
ActionView::Template::Error:
undefined method `<<' for #<Proc:0x00000013102d28>
# ./app/helpers/mailer_helper.rb:88:in `link_username'
# ./app/views/user_mailer/calendar_item_notification.html.erb:137:in `_app_views_user_mailer_calendar_item_notification_html_erb___206027131771876632_159436760'
# ./app/mailers/user_mailer.rb:136:in `calendar_item_notification'
# ./spec/mailers/user_mailer_spec.rb:19:in `block (4 levels) in <top (required)>'
# ./spec/mailers/user_mailer_spec.rb:19:in `block (3 levels) in <top (required)>'
# ./spec/support/misc.rb:13:in `block in suppress_output'
# ./spec/support/misc.rb:12:in `tap'
# ./spec/support/misc.rb:12:in `suppress_output'
# -e:1:in `<main>'
Thanks

undefined method `header` in RSpec

To learn API by using Rails I'm reading this tutorial.
In a part of RSpec test there is a method like this:
spec/support/authentication_helper.rb
module AuthenticationHelper
def sign_in(user)
header('Authorization', "Token token=\"#{user.authentication_token}\", email=\"#{user.email}\"")
end
def create_and_sign_in_user
user = FactoryGirl.create(:user)
sign_in(user)
user
end
alias_method :create_and_sign_in_another_user, :create_and_sign_in_user
end
RSpec.configure do |config|
config.include AuthenticationHelper, type: :api
end
And the test failed by undefined method `header'.
Where is this header method defined?
This is the whole source code of this tutorial.
https://github.com/vasilakisfil/rails_tutorial_api/
spec/apis/users_spec.rb
require 'rails_helper'
describe Api::V1::UsersController, type: :api do
context :show do
before do
create_and_sign_in_user
#user = FactoryGirl.create(:user)
get api_v1_user_path(#user.id), format: :json
end
it 'returns the correct status' do
expect(last_response.status).to eql(200)
end
it 'returns the data in the body' do
body = HashWithIndifferentAccess.new(MultiJson.load(last_response.body))
expect(body[:user][:name]).to eql(#user.name)
expect(body[:user][:updated_at]).to eql(#user.updated_at.iso8601)
end
end
end
StackTrace
1) Api::V1::UsersController show returns the correct status
Failure/Error: create_and_sign_in_user
NameError:
undefined local variable or method `request' for #<RSpec::ExampleGroups::ApiV1UsersController::Show:0x007fcbfec91d60>
# ./spec/support/authentication_helper.rb:4:in `sign_in'
# ./spec/support/authentication_helper.rb:9:in `create_and_sign_in_user'
# ./spec/apis/user_spec.rb:6:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:39:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:38:in `block (2 levels) in <top (required)>'
# -e:1:in `<main>'
2) Api::V1::UsersController show returns the data in the body
Failure/Error: create_and_sign_in_user
NameError:
undefined local variable or method `request' for #<RSpec::ExampleGroups::ApiV1UsersController::Show:0x007fcbfb7cfa28>
# ./spec/support/authentication_helper.rb:4:in `sign_in'
# ./spec/support/authentication_helper.rb:9:in `create_and_sign_in_user'
# ./spec/apis/user_spec.rb:6:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:39:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:38:in `block (2 levels) in <top (required)>'
# -e:1:in `<main>'
I had to add api_helper.rb to use the methods.
module ApiHelper
include Rack::Test::Methods
def app
Rails.application
end
end
RSpec.configure do |config|
config.include ApiHelper, type: :api #apply to all spec for apis folder
config.include Rails.application.routes.url_helpers, type: :api
end
Here is source code in Github.
https://github.com/vasilakisfil/rails_tutorial_api/blob/008af67e88897a5bcde714ce13d39a26ec70fba7/spec/support/api_helper.rb
In spec/support/auth_helpers.rb, you can try something like this
module AuthHelpers
def authenticate_with_user(user)
request.headers['Authorization'] = "Token token=#{user.token}, email=#{user.email}"
end
def clear_authentication_token
request.headers['Authorization'] = nil
end
end
In Rspec's spec/rails_helper.rb
Rspec.configure do |config|
config.include AuthHelpers, file_path: /spec\/apis/
end
An example test in spec/apis/users_controller_spec.rb:
require 'rails_helper'
describe Api::V1::UsersController, type: :controller do
let(:user) { create(:user) }
context 'signed in' do
before do
authenticate_with_user user
end
it 'does something' # tests here
end
end
Hope it helps!
Edit: Note the type: :controller is important

rspec_api_documentation - undefined method `call' for nil:NilClass

I've just started using rspec_api_documentation and cannot get it to work with my api. Created simple spec, as basic as first example in the readme but it keeps throwing undefined method 'call' for nil:NilClass error at me, and I have no idea what may cause the problem.
Here is full stack trace:
1) Users GET /api/v1/users Listing users
Failure/Error: do_request
NoMethodError:
undefined method `call' for nil:NilClass
# /home/swistak/.rvm/gems/ruby-2.2.2/gems/rack-test-0.6.3/lib/rack/mock_session.rb:30:in `request'
# /home/swistak/.rvm/gems/ruby-2.2.2/gems/rack-test-0.6.3/lib/rack/test.rb:244:in `process_request'
# /home/swistak/.rvm/gems/ruby-2.2.2/gems/rack-test-0.6.3/lib/rack/test.rb:58:in `get'
# /home/swistak/.rvm/gems/ruby-2.2.2/gems/rspec_api_documentation-4.7.0/lib/rspec_api_documentation/rack_test_client.rb:38:in `do_request'
# /home/swistak/.rvm/gems/ruby-2.2.2/gems/rspec_api_documentation-4.7.0/lib/rspec_api_documentation/client_base.rb:42:in `process'
# /home/swistak/.rvm/gems/ruby-2.2.2/gems/rspec_api_documentation-4.7.0/lib/rspec_api_documentation/client_base.rb:12:in `get'
# ./spec/acceptance/users_spec.rb:7:in `block (3 levels) in <top (required)>
Here is the spec
require 'spec_helper'
require 'rspec_api_documentation/dsl'
RSpec.resource 'Users' do
get '/api/v1/users' do
example 'Listing users' do
do_request
status.should == 200
end
end
end
and here is the controller
class Api::V1::UsersController < ApplicationController
skip_before_action :authenticate_with_token, only: %i(index show create)
expose(:users)
def index
render json: users, each_serializer: ::V1::UserSerializer
end
end
My api is under localhost:3000/api/v1/users
Any help appreciated
It seems that configuration block is required, so I've just created acceptance_helper.rb as in the example application and imported it in spec.

Render activeadmin view in rspec view spec?

I'm having issue with rendering an active admin view
RSpec.describe "active_admin/resource/new" do
it "is just simple test" do
render
end
end
but it returns
Failure/Error:
render# template: 'active_admin/resource/new.html.arb'
ActionView::Template::Error:
undefined method `renderer_for' for :Arbre::Context
# ./spec/views/admin/form_spec.rb:7:in `block (2 levels) in <top (required)>'
How to properly render it so I can do some tests on its content
I think this could be a starting point: https://github.com/activeadmin/activeadmin/blob/master/spec/unit/views/pages/form_spec.rb

Rails 4, Rspec dryly testing routes

I am trying to dry up my testing and would like to check routes. app.send("home_path") returns "/home" in the console but when I run it in tests it comes back as an undefined method.
Any ideas?
1) StaticPages Check paths should have content Home
Failure/Error: visit app.send("#{term.downcase}_path")
NoMethodError:
undefined method `home_path' for #<test::Application:0x007f917a48e078>
# ./spec/requests/static_pages_spec.rb:18:in `block (4 levels) in <top (required)>'
["Home", "About"].each do |term|
describe "Check paths" do
before(:each) do
visit app.send("#{term.downcase}_path")
end
it "should have content #{term}" do
expect(page).to have_content(term)
end
end
end

Resources