Why are helper methods not working in view specs? - ruby-on-rails

I have a conditional in my view based upon a helper method defined in my application controller, which I defined as a helper method.
<% if logged_in? %>
When my spec hits this, it says:
ActionView::Template::Error:
undefined method `logged_in?' for #<#<Class:0x007fdc06b71aa8>:0x007fdc0b9b0930>
When I use my browser, it works perfect. I tried stubbing it, but I get another error when I try to stub it on the view or controller. This is what my test looks like:
it "has a logout link" do
render
expect(rendered).to have_link "Logout"
end
Here's my gemfile:
ruby "2.4.1"
gem "bootsnap", ">= 1.1.0", require: false
gem "coffee-rails", "~> 4.2"
gem "jbuilder", "~> 2.5"
gem "pg"
gem "puma", "~> 3.11"
gem "rails", "~> 5.2.1"
gem "rails-controller-testing"
gem "sass-rails", "~> 5.0"
gem "turbolinks", "~> 5"
gem "uglifier", ">= 1.3.0"
group :development, :test do
gem "byebug", platforms: %i[mri mingw x64_mingw]
end
group :development do
gem "listen", ">= 3.0.5", "< 3.2"
gem "spring"
gem "spring-watcher-listen", "~> 2.0.0"
gem "web-console", ">= 3.3.0"
end
group :test do
gem "capybara", ">= 2.15"
gem "chromedriver-helper"
gem "factory_bot_rails"
gem "rspec-rails", "~> 3.7"
gem "selenium-webdriver"
gem "shoulda-matchers", "~> 3.1"
end

Update to the accepted answer, it now seems necessary to use without_partial_double_verification to get around the ActionView::Base does not implement: field_name error.
Example code:
let(:logged_in) { true }
before(:each) do
without_partial_double_verification do
allow(view).to receive(:logged_in?).and_return(logged_in)
end
render
end
it 'has a logout link' do
expect(rendered).to have_link 'Logout'
end
context 'with no logged user' do
let(:logged_in) { false }
it 'has a login link' do
expect(rendered).to have_link 'Login'
end
end

View specs usually does not need to call the real helper (if you wan't to test the helper then you can do a helper spec).
It's faster and easier to just stub the helper method to return the value you want for your given test:
context 'with a logged user' do
before(:each) do
allow(view).to receive(:logged_in?).and_return(true)
end
it 'has a logout link' do
render
expect(rendered).to have_link 'Logout'
end
end
context 'with no logged user' do
before(:each) do
allow(view).to receive(:logged_in?).and_return(false)
end
it 'has a login link' do
render
expect(rendered).to have_link 'Login'
end
end
When you do model, controller, views, helper or any other isolated test, it does not have access to all the app since it's designed to run fast. For example: controller tests does not render views by default, model specs only tests the model, view specs do not have a context of the request nor session, etc.
You can have full real user tests using integration (feature) specs which runs the whole user interaction from beginning to end with access to everything, even controlling what the user clicks.

Related

While using rspec i am constantly getting the same error

I am getting the same Error on running rspec that is :-
"Undefined Method - authorize_manage_partner_links"
I am using CANCAN gem in my Gemfile for authorization purposes and i am using Rails version 3.2 and Ruby version 1.9.2.
Please suggest what to do as i am totally new to rspec and rails. I am using a testing database, on importing the database for the first time, when i am running rspec it is showing successful but after that it is showing this error on running the same rspec file. I am using FactoryGirl. and i have defined everything in my factoryrb file as well.
This is my controller--
require 'will_paginate/array'
class PartnerLinksController < ApplicationController
#load_and_authorize_resource
before_filter :authorize_manage_partner_links!
layout "backoffice"
def index
#partner_links = PartnerLink.all
if params[:sortby]
#partner_links = #partner_links.sort_by {|p| p.name} if params[:sortby] == "up"
#partner_links = #partner_links.sort_by {|p| p.name}.reverse if params[:sortby] == "down"
else
#partner_links = #partner_links.sort_by {|p| p.name}
end
total = #partner_links.size
#partner_links = #partner_links.paginate(:page => params[:page], :per_page => 20)
page = params[:page]
if !page
page = 1
end
render :locals => {:total => total, :page => page, :sortby => params[:sortby], :partner_link => #partner_link}
end
def new
#partner_link = PartnerLink.new
end
def edit
#partner_link = PartnerLink.find(params[:id])
end
def create
#partner_link = PartnerLink.new(params[:partner_link])
if #partner_link.save
redirect_to partner_links_url
else
render "new"
end
end
def update
#partner_link = PartnerLink.find(params[:id])
if #partner_link.update_attributes(params[:partner_link])
redirect_to partner_links_url
else
render "edit"
end
end
def destroy
#partner_link = PartnerLink.find(params[:id])
#partner_link.destroy
redirect_to partner_links_url
end
end
This is my rspec controller that i am using--
require 'spec_helper'
describe PartnerLinksController do
before do
FactoryGirl.create(:partner_link)
role_permission
admin_role
user
currency
category
merchant
cart
deal
sign_in user
end
let(:role_permission) {FactoryGirl.create(:role_permission)}
let(:currency) {FactoryGirl.create(:currency)}
let(:merchant) {FactoryGirl.create(:merchant)}
let(:cart) {FactoryGirl.create(:cart)}
let(:deal) {FactoryGirl.create(:deal)}
let(:category) {FactoryGirl.create(:category)}
let(:authentication) {FactoryGirl.create(:authentication)}
let(:user) {FactoryGirl.create(:user)}
let(:admin_role) {FactoryGirl.create(:admin_role)}
describe "index" do
it "Should display all the partner links" do
get :index
end
end
describe "new" do
it "Should create all the partner links" do
get :new
end
end
describe "create" do
it "Should create new all the partner links" do
get :create
end
end
describe "update" do
it "Should update all the partner links" do
get :update
end
end
describe "destroy" do
it "Should delete all the partner links" do
get :destroy
end
end
end
app/models/partner_link.rb--
class PartnerLink < ActiveRecord::Base
# attr_accessible :title, :body
validates_presence_of :name, :url, :description
end
This is my Gemfile--
source 'http://rubygems.org'
ruby '1.9.2'
gem 'rails', '3.2.11'
gem 'authlogic'
gem "devise"
gem "devise-encryptable"
gem "devise-async"
gem 'omniauth'
gem 'omniauth-facebook'
gem 'omniauth-twitter'
gem "cancan"
gem 'activemerchant'
gem 'aws-sdk'
gem 'aws-s3'
gem 'paperclip', '~> 3.0'
gem 'oauth2'
#gem 'fastercsv' #no-longer required after ruby 1.9
gem 'pg'
#gem 'thin'
gem 'unicorn'
gem 'simple_form'
gem 'haml-rails' # Use HAML instead of ERB for templates
gem 'friendly_id'
gem 'will_paginate'
gem 'will_paginate-bootstrap'
gem "nifty-generators", :group => :development
#gem "pdfkit"
gem "pdfkit", "~> 0.5.0"
gem 'wkhtmltopdf-binary'
gem 'wicked_pdf'
gem 'sendgrid', "~> 1.0.1"
gem 'delayed_job'
gem 'delayed_job_active_record'
gem 'bootstrap-datepicker-rails'
#gem 'roadie' #fixing email css
group :development, :test do
gem 'rspec-rails'
gem 'guard-rspec'
# Database Cleaner clears the database between tests. This done because we have
# to set config.use_transactional_fixtures = false in the spec_helper file.
# The use_transactional_fixtures value is set to false because Selenium test
# driver cannot access the test records created via database transactions.
gem 'database_cleaner'
end
group :test do
# gem 'factory_girl_rails'
gem 'capybara'
# As we’re using Capybara we can call the save_and_open_page method at any
# point and this will open the page in a browser so that we can take a look
# at it. This is enabled by Launchy.
# gem 'launchy'
# gem 'rspec-mocks'
end
gem "less-rails"
gem 'twitter-bootstrap-rails', '>= 2.0.3'
gem 'rb-readline', '~> 0.5.0', require: 'readline'
gem 'execjs'
gem 'therubyracer'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', " ~> 3.2.3"
gem 'coffee-rails', "~> 3.2.1"
gem 'uglifier', '>= 1.0.3'
end
gem "mail", "~> 2.4.4"
gem 'jquery-rails'
#gem "mocha", :group => :test
gem 'gmaps4rails'
gem 'acts_as_xlsx'
gem "httparty"
gem 'newrelic_rpm'
gem 'font_assets'
gem 'paper_trail', '~> 3.0.6'
gem 'mailchimp-api', require: 'mailchimp'
gem 'rack-ssl-enforcer'
gem 'ckeditor'
gem "rails_best_practices"

record generated by FactoryGirl disappears in rspec

We are running a spec (3.4.4) with Rails 4.2.0. Here is the spec:
it "returns http success" do
proj = FactoryGirl.create(:ext_construction_projectx_project, :name => 'namenewnew')
get 'new' , {:project_id => proj.id}
expect(response).to be_success
end
In debug, proj.id is 3 and all its values are correct. ExtConstructionProjectx::Project.find(3) returns nil and ExtConstructionProjectx::Project.count returns 0. The project record just disappears.
Is it possible that the rspec did not see the project record created on its db connection? How to fix this problem?
Update:
Here is the gemspec
s.add_dependency "rails", ">=4.2.0"
s.add_dependency "jquery-rails"
s.add_dependency "simple_form"
s.add_dependency "will_paginate"
s.add_dependency "database_cleaner"
s.add_dependency "execjs"
s.add_dependency "sass-rails", '~>5.0.1'
s.add_dependency "coffee-rails", '~>4.1.0'
s.add_dependency "uglifier"
s.add_development_dependency "sqlite3"
s.add_development_dependency "rspec-rails", ">= 3.2.0"
s.add_development_dependency "factory_girl_rails", '~>4.5'
s.add_development_dependency 'capybara'
s.add_development_dependency 'launchy'
Also in rails_spec, there is:
config.use_transactional_fixtures = true
This is expected behavior. If database records persisted between test calls, your testing wouldn't be isolated or properly test each case.
If you don't want the current behavior:
Consider removing (or reconfiguring) the database_cleaner gem.
Set config.use_transactional_fixtures = false in your rspec configuration. Or review the rspec docs for another strategy.
The "better" solution:
Rewrite your tests such that they do not depend on state from earlier test runs.
Consider using before(:each) and before(:all) macros in your rspec tests.

Capybara::ElementNotFound: Unable to find xpath "/html" without webrat

I can't make Capybara to work in many of my integration tests.
When I visit certain pages, I get the following html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
And so when I try to use the have_content() selector, it raises the following error:
Failure/Error: page.should have_content("HELLO")
Capybara::ElementNotFound:
Unable to find xpath "/html"
Some of the pages of my apps I can visit just fine, but some other ones I can't. There are even some pages that work at some places, and not at others:
require 'spec_helper'
describe HomeController do
it "shows the website description" do
visit root_path
puts page.html.length # ==> 108 (no html)
...
end
end
require 'spec_helper'
describe FlowsController do
it "should do stuff" do
visit root_path
puts page.html.length # ==> 4459, works even if the exact same one didn't work in home_controller_spec!
visit flows_path
puts page.html.length # ==> 3402, works
visit new_user_session_path
puts page.html.length # ==> 3330, works
within("form#new_user") do
fill_in 'Email', :with => 'email#example.com'
fill_in 'Password', :with => 'password'
click_on 'Sign in'
end
puts page.html.length # ==> 108, No html
end
end
I read in this post that this was an error that might occur when using Capybara and webrat at the same time. But I'm not using webrat at all, and I still get the error...
Here is my Gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.6'
gem 'pg'
gem 'thin'
gem 'devise'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
# Add rspec for testing
group :test, :development do
gem "rspec-rails", "~> 2.0"
gem "capybara"
gem "factory_girl_rails"
end
gem 'jquery-rails'
Try passing an String to describe, instead of the Controller class
describe "whatever" do
...
end

Why cant I get selenium to kick in with capybara

Im trying to test a user clicking on a button which makes an ajax call. When i click it manuallly in my browser it behaves as expected i.e. default behaviour of the button is ignored and instead it gets the results via ajax which are then added to the page.
But when i run my tests using capybara, after clicking on the button it redirects to the buttons action. It seems selenium isnt kicking in. I cant figure out why.
Is it my config? Since it works in development mode Im assuming this isnt due to my jquery code so for brevity not displaying that.
Gemfile
source 'http://rubygems.org'
gem 'rails', '3.1.0.rc4'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
gem 'omniauth', '~>0.2.0'
gem 'pusher'
gem 'youtube_it'
gem 'simple_form'
# Asset template engines
gem 'sass-rails', "~> 3.1.0.rc"
gem 'coffee-script'
gem 'uglifier'
gem 'jquery-rails'
# Use unicorn as the web server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
group :test do
gem "shoulda"
gem "factory_girl_rails"
# Pretty printed test output
gem 'turn', :require => false
gem 'mocha'
end
group :development do
gem 'rails3-generators'
gem "autotest"
end
group :development, :test do
gem "capybara", :git => 'git://github.com/jnicklas/capybara.git'
gem "launchy"
gem "haml-rails"
gem "database_cleaner"
end
or my test_helper
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'shoulda/rails'
require "capybara/rails"
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all
OmniAuth.config.test_mode = true
# Add more helper methods to be used by all tests here...
def login_in(user)
#request.session[:user_id] = user.id
end
def should_redirect_unauthorized
assert_redirected_to root_path
assert_match /you need to login/i, flash[:alert]
end
end
module ActionController
class IntegrationTest
include Capybara::DSL
self.use_transactional_fixtures = false
setup do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.start #workaround for capybara / selenium. See capybara docs
end
teardown do
DatabaseCleaner.clean #workaround for capybara / selenium. See capybara docs
end
#signup using twitter, facebook for authentication
def signup_using(provider)
OmniAuth.config.add_mock(provider.to_sym, {'uid' => "123456"})
visit '/'
page.click_link("#{provider}_auth")
assert_match /\/users\/\d+\/edit/, current_path
assert page.find("#flash").has_content?("Welcome to")
end
#login into existing account using twitter, facebook
def login_using(service)
OmniAuth.config.add_mock(service.provider.to_sym, {'uid' => service.uid})
visit '/'
page.click_link("#{service.provider}_auth")
assert page.find("#flash").has_content?("Welcome back")
assert_equal rooms_path, current_path
end
def login_and_visit_room(service, room)
login_using(service)
visit_room(room)
end
def visit_room(room)
visit room_path(room)
assert_equal room_path(#room.id), current_path
end
end
end
or the setup blocks in my integration test
require 'test_helper'
class PlaylistStoriesTestTest < ActionDispatch::IntegrationTest
fixtures :all
setup do
Capybara.current_driver = :selenium
#user = Factory(:user)
#service = #user.services.create(:provider => "twitter", :uid => "123456")
#room = Factory(:room)
end
....
teardown do
Capybara.use_default_driver
DatabaseCleaner.clean #workaround for capybara / selenium. See capybara docs
end
end
With Capybara, you should not get confused with the difference between a link (even if it looks like a button) and a button (like "submit"). You did not provide the view file contents, but I guess, you are using a button, not a link.
With capybara, you have to differentiate
visit '/'
click_button 'Login'
or
visit '/'
click_link 'Home'
See also the Capybara-Documentation at GitHub

rspec giving incorrect error messages

Following the RailsTutorial, I keep getting this error message (one of four actually) when it should be working:
UsersController authentication of edit/update pages for signed-in
users should require matching users for 'edit'
Failure/Error: #users = [#user, second, third] undefined local variable
or method 'second' for
#<RSpec::Core::ExampleGroup::Nested_3::Nested_7::Nested_2:0x000001040dcb68> #
./spec/controllers/users_controller_spec.rb:275:in 'block
(4 levels) in <top (required)>'
I have tried to fix it by looking at other questions on stack overflow but to no avail. My gemfile now looks like this:
gem 'rails', '3.0.3'
gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
gem 'gravatar_image_tag', '0.1.0'
gem 'will_paginate', '3.0.pre2'
group :development do
gem 'rspec-rails', '2.0.1'
gem 'annotate-models', '1.0.4'
gem 'faker', '0.3.1'
end
group :test do
gem 'rspec-rails', '2.0.1'
gem 'webrat', '0.7.1'
gem 'spork', '0.8.4'
gem 'factory_girl_rails', '1.0'
end
Any suggestions would be greatly appreciated!
The backtrace is coming from the users_controller_spec. Without the source, it's hard to say, but I'm guessing it's something like this:
describe UsersController
before do
#user = Factory(:user)
sign_in(#user)
end
context "GET to edit for another user" do
before do
#other_user = Factory(:user)
get :edit, :id => #other_user
end
it "should disallow editing of other users" do
# ... expectations here of HTTP 401 or a user-friendly message
end
end
end
Note that the variables shared between #before blocks and the examples are instance variables - local variables wouldn't be shared across them. I'm guessing your code might be trying to share a local between examples, or between a #before block and its examples.
I'm guessing second and third don't exist when you're calling [#user, second, third] (well, more than guessing. Its what the error says ;) ). They're probably in the tutorial to suggest you could be handling multiple objects in this code. Try removing them.
If that's not it, can you post a link to the tutorial you're following, or provide more background about where second and third are supposed to be coming from?

Resources