I would like to test a controller that directly renders some JSON output (by using "render :json => #entity_names"). For that task I tried in my spec file "response.should have_text('["enim", "enita"]')". Unfortunately I always get that error:
Failure/Error: response.should have_text('["enim", "enita"]')
undefined method `has_text?' for #
Do I miss some gem that provides that method? Here my Gemfile:
source 'http://rubygems.org'
gem 'rails', '>= 3.0.0'
gem 'mysql2'
gem 'mongrel'
gem 'devise'
gem 'will_paginate', :git => 'git://github.com/mislav/will_paginate.git', :branch => 'rails3'
gem 'thinking-sphinx', :git => 'git://github.com/freelancing-god/thinking-sphinx.git', :branch => 'rails3', :require => 'thinking_sphinx'
group :test, :development do
gem 'rspec-rails', '>= 2.0.0.beta.19'
gem 'steak', :git => 'git://github.com/cavalle/steak.git'
gem 'webrat'
gem 'capybara'
gem 'capybara-envjs'
gem 'shoulda'
gem 'launchy'
gem 'autotest'
gem 'autotest-rails'
gem 'test_notifier'
gem 'rails3-generators'
gem 'factory_girl_rails'
gem 'populator'
gem 'faker'
gem 'random_data'
gem 'database_cleaner', :git => 'git://github.com/bmabey/database_cleaner.git'
gem 'delorean'
end
You could construct your expected output as JSON, then get the response body (which is also JSON), decode both, and compare them. Something like:
it "should do something" do
expected = { :some_key => "and some value" }.to_json
xhr :post, :create, { :foo => "bar" }
ActiveSupport::JSON.decode(response.body).should == ActiveSupport::JSON.decode(expected)
end
Related
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"
I am trying to test my controller by filling some values.i have placed my controller spec file under spec/feature. But my visit is not properly working, it is visiting wrong controller.
My controller spec file is
require 'spec_helper'
describe ApplicationsController, :type => :controller do
render_views
#the problem is - we need to test criminal record data block from saferent response
#this guy edwin avila is test input provided by saferent
describe "GET 'new'" , :type => :request do
before do
visit '/TestApplication'
fill_in 'application[applicants_attributes][0][first_name]', :with => 'Rose'
click_link_or_button 'sbmt'
end
it 'has 200 status code if logged in' do
expect(response.status).to eq(200)
end
end
end
My Gem file is
source 'https://rubygems.org'
gem 'rails', '4.0.0'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 4.0.0'
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
gem 'yui-compressor'
end
gem 'jquery-rails'
gem 'git'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
gem 'passenger'
# Deploy with Capistrano
gem 'capistrano'
gem 'rvm-capistrano'
# To use debugger
# gem 'debugger'
gem "xpath", "~> 2.0.0"
gem "formtastic"
gem 'simple_form', :git => 'git://github.com/plataformatec/simple_form.git'
gem "rentjuicer"
gem "capybara", :group => [:development, :test]
#gem "capybara", "~> 2.1.0"
gem "daemons"
gem "mongoid", github: "mongoid/mongoid"
#gem "delayed_job_mongoid" #upto rails 3
gem "bson_ext", '~> 1.5'
gem "whenever", :require => false
gem "bitly", "~> 0.8.0"
gem "mogreet"
gem 'wicked_pdf'
gem 'httparty'
gem 'activemodel'
gem 'actionpack','~> 4.0'
gem "carrierwave-mongoid", "~> 0.6.3"
gem 'mongoid-grid_fs', github: 'ahoward/mongoid-grid_fs'
#gem 'client_side_validations'
#gem 'client_side_validations-simple_form'
#gem "client_side_validations-mongoid", "~> 4.1.2"
gem 'twilio-ruby'
gem "rash", "~> 0.4.0"
gem "activeresource", "~> 4.0.0"
gem 'rb-readline', '~> 0.4.2'
gem 'googlecharts'
gem 'htmlentities'
gem 'rspec-rails' , "~> 2.0"
gem 'mongoid-rspec'
gem 'crack'
gem 'simplecov'
#gem 'whoops_rails_logger' upto rails 3
when i try to run
rspec spec/features/applications_controller_spec.rb
i got
Failures:
1) GET 'new' has 200 status code if logged in
Failure/Error: visit '/TestApplication'
NoMethodError:
undefined method `collect' for nil:NilClass
# ./app/controllers/home_controller.rb:8:in `index'
# ./spec/features/applications_controller_spec.rb:8:in `block (2 levels) in <top (required)>'
Finished in 0.14064 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/features/applications_controller_spec.rb:12 # GET 'new' has 200 status code if logged in
Randomized with seed 32162
my route file
mYAPP::Application.routes.draw do
match 'street/:id' => 'home#street', :as => :street, via: [:get,:post]
match '/pdf' => 'homes#pdf', via: [:get,:post]
match '/login' => 'homes#login', via: [:get,:post]
match '/changing_f_gal' =>'homes#changing_f_gal' , via: [:get,:post]
match '/saferent_test' => 'applications#newnew', via: [:get,:post]
match '/saferent_result' => 'applications#newcreate' , via: [:get,:post]
match '/showall' => 'applications#showall', via: [:get,:post]
match '/visitor/create_visits' => 'visitor#create_visits' , :via => :get
match '/visitor/init_visitor' => 'visitor#init_visitor' , :via => :get
match 'TestApplication' => 'applications#create', :via => :get
root :to => 'home#index'
end
Capybara's visit method won't work for that file path (assuming you're trying to get to the root of your application). Try something like
visit root_path
I'd recommend checking out the documentation at http://jnicklas.github.com/capybara
Before(:all) do
puts "HELLO ALL :D"
end
I have some problem with my code:
/spec/factories.rb
require 'faker'
FactoryGirl.define do
factory :booking_error do
booking_id { Faker::Number.number(3).to_i }
error_type_cd BookingError.error_types.values.shuffle.first
process_name Enums::FlightEnum::PROCESSES.keys.shuffle.first
description "DESCRIPTION"
old_value "OLD_STRING"
new_value "NEW_STRING"
end
end
/spec/models/booking_error_spec.rb
require 'spec_helper'
describe BookingError do
before(:all) do
#booking_error = FactoryGirl.build(:booking_error)
#booking_error_types = BookingError.error_types
end
it 'Validating BookingError save.' do
#booking_error.save.should be_true
end
end
Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.13'
gem 'mysql2' , '0.3.11'
gem 'devise' , '2.2.0.rc'
gem 'devise-encryptable' , '0.1.1'
gem 'unicorn' , '4.5.0'
gem 'kaminari' , '0.14.1'
gem 'memcache-client' , '1.8.5'
gem 'simple_enum' , '1.6.4'
gem 'resque' , '1.23.0' , :require => "resque/server"
gem 'resque-logger' , '0.1.0'
gem 'resque-workers-lock'
gem 'whenever' , '0.8.1' , :require => false
gem 'httparty' , '0.9.0'
gem 'newrelic_rpm' , '3.6.1.88'
gem 'cancan' , '1.6.8'
gem 'rolify' , '3.2.0'
gem 'json-schema' , '1.1.1'
gem 'faker' , '1.1.2'
gem 'ruby-enum' , '0.2.1'
gem 'mail' , '2.5.4'
gem 'daemons-rails'
group :develop, :test do
gem 'capistrano' , '2.13.5'
gem 'capistrano-unicorn' , '0.1.6' , :require => false
gem 'capistrano-resque' , '~> 0.1.0' , :require => false
gem 'rvm-capistrano' , '1.2.7'
gem 'capistrano-ext'
gem 'rspec-rails' , '2.12.0'
gem 'debugger' , '1.5.0'
gem 'thin' , '1.5.0'
gem 'annotate' , '2.5.0'
gem 'factory_girl_rails' , '4.1.0'
gem 'yard' , '0.8.5.2'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'therubyracer' , '0.10.2'
gem 'coffee-rails' , '3.2.2'
gem 'uglifier' , '1.3.0'
gem 'twitter-bootstrap-rails' , '2.1.9'
gem 'sass' , '3.2.4'
gem 'jquery-ui-rails' , '3.0.0'
gem 'less-rails' , '2.2.6'
end
gem 'jquery-rails' , '2.1.4'
When i run:
$ rspec spec/models/booking_error_spec.rb
I get the error that is in the title:
NameError: uninitialized constant Faker::Number
Number didn't exist in Faker at the version you are using (1.1.2). Update to version 1.2.0 and it will work.
I upgraded to rails4 from 3.2.2 and i now get this undefined method from my rabl template that id didn't get before the upgrade.
the "tooth" paramater on procedures is a set by a getter/setter method that is stored in the postgres database as a hstore hash named properties.
Not sure what has changed in rails4 to make this happen!
Any help would be much appreciated.
ERROR:
Showing /home/action/app/views/procedures/chart.json.rabl where line #1 raised:
undefined method `scan' for {"tooth"=>""}:Hash
Extracted source (around line #1):
1 object #procedures
2 attributes :buccal, :mesial, :occlusal, :distal, :lingual
Trace of template inclusion: app/views/procedures/_procedures_table.html.erb, app/views/procedures/chart.html.erb
Procedures model (getter and setter method for :tooth)
%w[tooth buccal mesial occlusal distal lingual].each do |key|
attr_accessible key
scope "has_#{key}", lambda { |value| where("properties #> (? => ?)", key, value) }
define_method(key) do
properties && properties[key]
end
define_method("#{key}=") do |value|
self.properties = (properties || {}).merge(key => value)
end
end
Gemfile:
source 'http://rubygems.org'
gem 'rails', '4.0.0.rc2'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'pg'
gem "paperclip", "~> 3.0"
gem 'rabl'
gem 'activerecord-postgres-hstore'
gem "bugsnag"
gem 'country_select'
gem 'bullet', group: :development
# Asset template engines
gem 'sass-rails', '~> 4.0.0.rc1'
gem 'coffee-rails', '~> 4.0.0.rc1'
gem 'uglifier', '>= 1.3.0'
gem 'twitter-bootstrap-rails'
gem 'therubyracer', :platform => :ruby
gem 'less-rails'
gem 'jquery-ui-rails'
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'
# Declarative Authorization
gem 'declarative_authorization'
gem 'protected_attributes'
# Authlogic
gem 'authlogic'
gem 'simple_form', '3.0.0.rc'
group :production do
end
group :test do
# Pretty printed test output
gem 'turn', :require => false
end
gem 'rubber'
gem 'open4'
gem 'gelf'
gem 'graylog2_exceptions', :git => 'git://github.com/wr0ngway/graylog2_exceptions.git'
gem 'graylog2-resque'
Found the answer!! Rails 4 now has native support for Hstore, which is awesome but I hadn't realised. To fix my problem i merely followed this guide below which is great to set up my model with hstore and then deleted my getter/setter methods. http://blog.remarkablelabs.com/2012/12/a-love-affair-with-postgresql-rails-4-countdown-to-2013
The following console logs should describe the issue
bundle install
Could not find multi_json-1.1.0 in any of the sources
Run `bundle install` to install missing gems.
bundle install asks me to bundle install.
Have tried: https://github.com/mpapis/rubygems-bundler/issues/4
Looked at: https://github.com/carlhuda/bundler/issues/865
Not sure where to go next.
Also of interest, if we comment all the gems from inside vendor/gems the bundle works fine.
source 'http://rubygems.org'
gem 'rails', '~> 3.2.2'
gem 'mysql2'
gem 'thin'
# Asset template engines
gem 'coffee-script'
gem 'uglifier'
gem 'sprockets'
gem 'less-js'
gem 'execjs'
gem 'nokogiri'
gem 'lucy'
gem 'babilu'
gem 'simple_form'
gem 'mechanize'
gem 'geoip'
gem 'less'
gem 'bb-ruby'
gem 'client_side_validations', "~> 3.2.0.beta.2"
gem 'client_side_validations-simple_form'
gem 'require_all'
#for avatars
gem 'paperclip'
gem 'rmagick'
gem "multipart-post"
gem 'gibberish'
# Memcached client
gem 'dalli'
#to support tables without auto-incrementing primary keys
gem 'composite_primary_keys', "~> 5.0.1"
gem 'calendar_helper', :require => 'calendar_helper'
gem 'jquery-rails'
gem 'capistrano'
gem 'capistrano-ext'
gem 'simple_form'
gem 'htmlentities'
gem 'will_paginate'
gem 'airbrake', :path => "vendor/gems/airbrake"
gem "uservoice", :path => "vendor/gems/uservoice"
# For spambots
gem "honeypot-captcha"
# Internationalization GUI
gem "tolk", :git => "http://github.com/miloops/tolk.git", :branch => "rails31"
gem "will-paginate-i18n"
gem "omniauth"
gem "omniauth-facebook"
gem "omniauth-justintv", :git => 'http://github.com/themindoverall/omniauth-justintv.git'
gem 'oauth'
gem 'rails-i18n'
# For rate limiting
gem "rack-throttle", :path => "vendor/gems/rack-throttle"
gem "xfire-api", :path => "vendor/gems/xfire-api"
gem "anametrix", :path => "vendor/gems/anametrix"
#To make ruby debug work see: http://stackoverflow.com/questions/6438116/rails-with-ruby-debugger-throw-symbol-not-found-ruby-current-thread-loaderro
group :development, :test do
#gem 'ruby-debug-base19', '0.11.26', :path => "~/.rvm/gems/ruby-1.9.3-p#{RUBY_PATCHLEVEL}/gems/ruby-debug-base19-0.11.26/"
#gem 'linecache19', '0.5.13', :path => "~/.rvm/gems/ruby-1.9.3-p#{RUBY_PATCHLEVEL}/gems/linecache19-0.5.13/"
#gem 'ruby-debug19', :require => 'ruby-debug'
gem 'ruby_parser'
end
group :test do
gem 'ruby-prof'
gem 'test-unit' # For performance testing
gem 'turn', :require => false
gem 'factory_girl_rails', "~> 1.0.1"
gem 'capybara', "~> 1.0.0"
gem 'database_cleaner', '~> 0.6.7'
gem 'rspec-rails', "~> 2.6.1"
gem 'cucumber-rails', "~> 1.0.0"
gem 'webrat', "~> 0.7.3"
gem 'autotest'
gem 'simplecov'
gem 'autotest-rails-pure'
gem 'simplecov', :require => false, :group => :test
gem 'spork'
end
group :production do
gem 'rack-google_analytics', :require => "rack/google_analytics"
end
Have you declared that gem in your Gemfile? Sometimes it helps to specify a specific version as you've done for some:
gem 'multi-json', '1.1.0'
If anyone turns out to be stuck on this what turned out to be the answer is one of our homegrown gems:
gem "xfire-api", :path => "vendor/gems/xfire-api"
had a require in it provided by a gem that was not specified in the gemspec. If you're stuck on this and have homegrown gems take a look through all your requires and makes sure all the files are provided by gems you specify in the Gemspec.