Rspec-Rails-Capybara. "Response" object - ruby-on-rails

I am reading M. Hart's Rails tutorial book now. Stopped at 9.2.1 because of error in testing redirection after patch request. Use Ruby on Rails 4.1 framework, my app's gems:
ruby '2.0.0'
gem 'rails', '4.1.0'
...
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
gem 'factory_girl_rails', '4.2.1'
gem 'rspec-rails', '2.13.1'
I have UsersController
class UsersController < ApplicationController
before_action :signed_in_user, only: [:edit, :update]
#actions...
private
#strong parameters...
def signed_in_user
redirect_to signin_url, notice: "Please sign in." unless signed_in?
end
end
My Rspec+Capybara test for redirection:
describe "Authentication" do
subject { page }
describe "authorization" do
describe "for non-signed-in users" do
let(:user) { FactoryGirl.create(:user) }
describe "in the Users controller" do
#...
describe "submitting to the update action" do
before { patch user_path(user) }
specify { expect(response).to redirect_to(signin_path) }
end
end
#...
end
And when I run rspec spec/
1) Authentication authorization for non-signed-in users in the Users controller submitting to the update action
Failure/Error: specify { expect(response).to redirect_to(signin_path) }
NoMethodError:
undefined method `assertions' for #<RSpec::Rails::TestUnitAssertionAdapter::AssertionDelegator:0xae34810>
# ./spec/requests/authentication_pages_spec.rb:23:in `block (6 levels) in <top (required)>'
What's wrong?

Use:
gem "rspec-rails", '~> 2.14.0'
as per this previously answered question:
Rails 4 and RSpec, undefined method `assertions' in routing spec
Note that the gem is no longer an rc, so you should be able to use the release version.

Related

RSpec error "undefined method `respond_with'..."

I am following the tutorials http://apionrails.icalialabs.com/book/chapter_three
But when I ran the controller test bundle exec rspec spec/controllers I am getting undefined method error:
Failures:
Api::V1::UsersController GET #show
Failure/Error: it { should respond_with 200 }
NoMethodError:
undefined method `respond_with' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x00000005d6f650>
# ./spec/controllers/api/v1/users_controller_spec.rb:17:in `block (3 levels) in <top (required)>'
Finished in 0.08435 seconds
2 examples, 1 failure
Please help
Edit:
The below is the contents of my spec file
users_controller_spec.rb
require 'spec_helper'
describe Api::V1::UsersController do
before(:each) { request.headers['Accept'] = "application/vnd.marketplace.v1" }
describe "GET #show" do
before(:each) do
#user = FactoryGirl.create :user
get :show, id: #user.id, format: :json
end
it "returns the information about a reporter on a hash" do
user_response = JSON.parse(response.body, symbolize_names: true)
expect(user_response[:email]).to eql #user.email
end
it { should respond_with 200 }
end
end
Here since we are dealing with response codes you can use-
it { expect(response).to have_http_status(200) }
instead of it { should respond_with 200 }
Here no need of shoulda-matchers gem as well.
Ref - https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs
I had the same problem and solved it. It looks like using older version of shoulda-matchers helps. I changed the Gemfile to gem "shoulda-matchers", "~>2.5"
Having this error after upgrade my old rails app to rails 5.0. then add the following configuration into spec_helper.rb and now its working fine.
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
If you're not finding shoulda methods, I'd make sure you have this in your rails_helper.rb or spec_helper.rb:
config.include(Shoulda::Matchers::ActionController, { type: :model, file_path: /spec\/controllers/})
Or it might be there but commented-out.
This seems to be an error from shoulda-matchers gem, try to update to the latest version.
Also make sure that it comes after RSpec in your gemfile
group :development, :test do
gem 'rspec-rails'
gem 'shoulda-matchers'
end

rails rspec tests for lib class used by routes contains private methods

Simplecov detected that I was missing some tests on my lib/api_verson.rb class:
class ApiVersion
def initialize(version)
#version = version
end
def matches?(request)
versioned_accept_header?(request) || version_one?(request)
end
private
def versioned_accept_header?(request)
accept = request.headers['Accept']
accept && accept[/application\/vnd\.#{Rails.application.secrets.my_app_accept_header}-v#{#version}\+json/]
end
def unversioned_accept_header?(request)
accept = request.headers['Accept']
accept.blank? || accept[/application\/vnd\.#{Rails.application.secrets.my_app_accept_header}/].nil?
end
def version_one?(request)
#version == Rails.application.secrets.my_app_default_api_version && unversioned_accept_header?(request)
end
end
This class is used by the routes file to help setup api versions:
namespace :api, path: "", defaults: {format: :json} do
scope module: :v1, constraints: ApiVersion.new(1) do
get '/alive', to: 'api#alive'
end
scope module: :v2, constraints: ApiVersion.new(2) do
get '/alive', to: 'api#alive'
end
end
This setup was ported from versioning_your_ap_is.
I am trying to test the methods here that simplecov is reporting as failures:
require 'spec_helper'
describe ApiVersion do
before(:each) do
#apiversion = ApiVersion.new(1)
#request = ActionController::TestRequest.new(host: 'localhost')
#request.headers["Accept"] = "application/vnd.#{Rails.application.secrets.my_app_accept_header}-#{Rails.application.secrets.my_app_default_api_version}+json"
end
describe 'Method #versioned_accept_header =>' do
it 'Should return the correct accept header version' do
binding.pry
end
end
end
I am trying to build this first test to attempt??? to #apiversion.send(:unversioned_accept_header, #request) and I am getting the error:
#apiversion.send(:unversioned_accept_header, #request)
NoMethodError: undefined method `unversioned_accept_header' for #<ApiVersion:0x007fae009bdad8 #version=1>
from (pry):1:in `block (3 levels) in <top (required)>'
Basically the following methods are flagged: "matches?, versioned_accept_header?, unversioned_accept_header?, and version_one?"
I am not the rockstar at rspec and could use some pointers here. Thank you for your help.
Btw, this is a rails 4 application running:
group :development, :test do
gem 'pry'
gem 'pry-doc'
gem 'pry-debugger'
gem 'pry-rails'
gem 'pry-plus'
gem 'pry-rescue'
gem 'pry-stack_explorer'
gem 'pry-clipboard'
gem 'pry-nav'
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'faker'
gem 'seedbank'
gem 'capybara'
end
group :test do
gem 'simplecov', '~> 0.7.1'
gem 'shoulda-matchers'
gem 'spork-rails'
gem 'database_cleaner'
gem 'email_spec'
gem 'timecop'
gem 'json_spec'
end
You run this code
#apiversion.send(:unversioned_accept_header, #request)
but you have method:
def unversioned_accept_header?(request)
accept = request.headers['Accept']
accept.blank? || accept[/application\/vnd\.#{Rails.application.secrets.my_app_accept_header}/].nil?
end
try change
#apiversion.send(:unversioned_accept_header, #request) -> #apiversion.send(:unversioned_accept_header?, #request)
or change method name
def unversioned_accept_header? -> def unversioned_accept_header

Factory not registered: user (from Hartl tutorial 7.1.3)

When running bundle exec rspec on my sample rails app from the Michael Hartl tutorial I'm getting error `Argument Error: Factory not registered: user'
I have seen a bunch of extremely similar questions asked but I have tried the two solutions I have seen which are
a) Reboot the rails server
b) Install 'factory_girl_rails"
Relevant sections of my Gemfile :
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.0'
gem 'bootstrap-sass', '2.3.2.0'
gem 'bcrypt-ruby', '3.0.1'
group :test do
gem 'selenium-webdriver', '2.0.0'
gem 'capybara', '2.1.0'
gem 'factory_girl_rails', '4.2.1'
end
Facotries.rb
#root/spec/factories.rb
FactoryGirl.define do
factory :user do
name "Michael Hartl"
email "michael#example.com"
password "foobar"
password_confirmation "foobar"
end
end
user_pages_spec.rb
#root/spec/requests/user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_content(user.name) }
it { should have_title(user.name) }
end
describe "signup page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title('Sign up') }
end
end
show.html.erb
#root/app/views/user/show.html.erb
<% provide(:title, #user.name) %>
<h1><%= #user.name %></h1>
Error output
Failures:
1) User pages profile page
Failure/Error: let(:user) { FactoryGirl.create(:user) }
ArgumentError:
Factory not registered: user
# ./spec/requests/user_pages_spec.rb:8:in `block (3 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
I've never ceded to the stack overflow gods before but I'm about to toss my computer out of a window. Help is much appreciated!
Bigxiang's comment is the solution. I'm a beginner and actually didn't know that the names of my directories were important. A typo was causing the problem.

Undefined method 'patch' for #<Rspec::Core - Rails Tutorial Chapter 9

I'm getting the following error, and can't figure out how to fix it. There are some other pages on Stack Overflow addressing similar issues, but these don't seem to apply, or at least I'm not savvy enough to work it out.
When I run the authentication_pages_spec, the issue appears to be with the use of "patch", resulting in the failure below. Note that I've experimented with replacing "patch" with "put" and get the same results.
Failures:
1) Authentication authorization for non-signed-in users in the Users controller submitting to the update action
Failure/Error: before { patch user_path(user) }
NoMethodError:
undefined method `patch' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_3::Nested_1::Nested_1::Nested_2:0x007fa7e4df1e50>
# ./spec/features/authentication_pages_spec.rb:59:in `block (6 levels) in <top (required)>'
Finished in 0.34392 seconds
2 examples, 1 failure
NOTE: based on discussion on Stack Overflow here and the blog it references here I elected to change my spec/requests folder to spec/features, since this appears to be what Capybara requires after version 2.0.
Here is my Gemfile:
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.0'
gem 'bootstrap-sass', '2.3.2.0'
gem 'pg', '0.15.1'
gem 'bcrypt-ruby', '3.0.1'
group :development, :test do
gem 'rspec-rails', '2.13.1'
gem 'guard-rspec', '2.5.0'
gem 'spork-rails', github: 'sporkrb/spork-rails'
gem 'guard-spork', '1.5.0'
gem 'childprocess', '0.3.9'
end
group :test do
gem 'selenium-webdriver', '2.0.0'
gem 'capybara', '2.1.0'
gem 'growl', '1.0.3'
gem 'factory_girl_rails', '4.2.1'
gem 'cucumber-rails', '1.3.0', :require => false
gem 'database_cleaner', github: 'bmabey/database_cleaner'
end
gem 'sass-rails', '4.0.0'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.0'
gem 'jquery-rails', '2.2.1'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
I modified my Guardfile to use the spec/features folder instead of the spec/requests folder:
require 'active_support/inflector'
guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' },
:rspec_env => { 'RAILS_ENV' => 'test' } do
watch('config/application.rb')
watch('config/environment.rb')
watch('config/environments/test.rb')
watch(%r{^config/initializers/.+\.rb$})
watch('Gemfile')
watch('Gemfile.lock')
watch('spec/spec_helper.rb') { :rspec }
watch('test/test_helper.rb') { :test_unit }
watch(%r{features/support/}) { :cucumber }
end
guard 'rspec', all_after_pass: false, cli: '--drb' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
# Capybara features specs
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
# Custom Rails Tutorial specs
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) do |m|
["spec/routing/#{m[1]}_routing_spec.rb",
"spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb",
"spec/acceptance/#{m[1]}_spec.rb",
(m[1][/_pages/] ? "spec/features/#{m[1]}_spec.rb" :
"spec/features/#{m[1].singularize}_pages_spec.rb")]
end
watch(%r{^app/controllers/sessions_controller\.rb$}) do |m|
"spec/features/authentication_pages_spec.rb"
end
end
Users controller here:
class UsersController < ApplicationController
attr_accessor :name, :email
before_action :signed_in_user, only: [:edit, :update]
def show
#user = User.find(params[:id])
end
def new
#user = User.new
end
def create
#user = User.new(user_params)
if #user.save
sign_in #user
flash[:success] = "Welcome to the Sample App!"
redirect_to #user
else
flash[:error] = "Oops!"
render 'new'
end
end
def edit
#user = User.find(params[:id])
end
def update
#user = User.find(params[:id])
if #user.update_attributes(user_params)
flash[:success] = "Profile updated"
sign_in #user
redirect_to #user
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name,
:email, :password, :password_confirmation)
end
# Before filters
def signed_in_user
redirect_to signin_url, notice: "Please sign in." unless signed_in?
end
end
Spec helper file here:
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.include Capybara::DSL
# Added below myself, troubleshooting an 'undefined method 'patch'' message
config.include Rails.application.routes.url_helpers
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
Finally, for good measure, the config/routes.rb file is here:
SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
There is a similar discussion on Stack Overflow here, but the answer on that post is to move the tests from ./spec/requests to ./spec/features - which I've already done.
Another related discussion here, where the answer is to use require 'spec_helper' in the top of the feature file...which I already have.
One last observation: seems like much of the discussion on this topic indicates the issue is with Capybara needing tests located in the spec/features folder for it to be able to access the URL helpers - but my authentication_pages_spec.rb file seems to have no problem with 'visit'.
Appreciate any insight! The Rails Tutorial for Rails 4.0 is in beta, have been trying to figure out if this is a bug.
* UPDATE *
Got everything passing green after implementing Peter's suggestion. Following is the revised code from the authentication_pages_spec file (partial, just the "authorization" section):
describe "authorization", type: :request do
.
.
.
describe "as wrong user" do
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) {FactoryGirl.create(:user, email: "wrong#example.com") }
before { sign_in user, no_capybara: true }
describe "visiting Users#edit page" do
before { visit edit_user_path(wrong_user) }
it { should_not have_title(full_title('Edit user')) }
end
describe "submitting a PATCH request to the Users#update action" do
before { patch user_path(wrong_user) }
# NOTE: Rails Tutorial uses root_url below not root_path
specify { expect(response).to redirect_to(root_path) }
end
end
end
end
get, put, patch, etc. are only defined for :controller and :request specs, not for a :feature spec, which your authentication_pages_spec.rb test implicitly became when you changed its location. If you include type: :request in your top level describe, it should work.
this answer also fixes this rspec test failing without error which is part of the Rails Tutorial Chapter 9 (spec/requests/authentication_pages_spec.rb)
describe "submitting to the update action" do
before { patch user_path(user) }
specify { response.should redirect_to(signin_path) }
end

railstutorial.org - undefined method `Factory'

I'm attempting to follow railstutorial.org, and am currently on Chapter 7, where you start using factories: http://railstutorial.org/chapters/modeling-and-viewing-users-two#sec:tests_with_factories
I'm using Rails 3.0.1 and ruby-1.9.2-p0
I can't for the life of me get my rspec tests to pass though, the error i get is
Failures:
1) UsersController GET 'show' should be successful
Failure/Error: #user = Factory(:user)
undefined method `Factory' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x00000101cc5608>
# ./spec/controllers/users_controller_spec.rb:9:in `block (3 levels) in <top (required)>'
my factories.rb looks like this:
# By using the symbol ':user', we get Factory Girl to simulate the User model.
Factory.define :user do |user|
user.name "Michael Hartl"
user.email "mhartl#example.com"
user.password "foobar"
user.password_confirmation "foobar"
end
and this is my users_controller_spec.rb file:
require 'spec_helper'
describe UsersController do
render_views
describe "GET 'show'" do
before(:each) do
#user = Factory(:user)
end
it "should be successful" do
get :show, :id => #user
response.should be_success
end
here is my Gemfile, if it helps:
source 'http://rubygems.org'
gem 'rails', '3.0.1'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3-ruby', :require => 'sqlite3'
gem 'gravatar_image_tag'
group :development do
gem 'rspec-rails'
gem 'annotate-models'
end
group :test do
gem 'rspec'
gem 'webrat'
gem 'spork'
gem 'factory_girl_rails'
end
As per the latest version of Factory Girl (currently v4.0.0)
rewrite factories.rb
FactoryGirl.define do
factory :user do
name "Michael Hartl"
email "mhartl#example.com"
password "foobar"
password_confirmation "foobar"
end
end
then call it from your users controller specs as:
FactoryGirl.create(:user)
I got this exact same error message. I just restarted my Spork server and Autotest and everything went green for me.
Maybe you should try the new syntax (see github readme of factory girl)
FactoryGirl.define :user do |user|
user.name "Michael Hartl"
user.email "mhartl#example.com"
user.password "foobar"
user.password_confirmation "foobar"
end
In your spec use
#user = FactoryGirl(:user)
instead of
#user = Factory(:user)
I had this problem, but it was because I had placed the factory girl gem under the development section instead of the test section of the Gemfile. Once under the test section, it worked. One difference I note between my entry and yours is that mine specifies 1.0:
group :test do
gem 'rspec-rails', '2.6.1'
gem 'webrat', '0.7.1'
gem 'factory_girl_rails', '1.0'
end
For me I had to add require 'factory_girl' to test_helper.rb
My solution: I've accidentally included it in the :development block, and simply had to move it to the :test block
(I've listed it here, because it might help someone who doesn't follow the tutorial correctly)
I have done so,
add require 'factory_girl' to test_helper.rb and
#user = FactoryGirl.create(:user)
For those finding this page now: note where you once used "FactoryGirl" you must now use "FactoryBot" in your tests. From the thoughtbot announcement page:
"We’re renaming factory_girl to factory_bot (and factory_girl_rails to
factory_bot_rails). All the same functionality of factory_girl, now
under a different name."
More details here:
https://robots.thoughtbot.com/factory_bot
I was determined to use the newest version of Factory Girl, so I tried to adapt the code. Didn't work for me, so I used
gem 'factory_girl_rails', '1.0'
in the Gemfile to lock the version at 1.0
bundle update
restart spork and autotest and it worked.

Resources