I've followed all of the steps that I've been able to find online for configuring Rails 3 with Rspec 2 and Mocha. In my Gemfile:
group :development do
gem 'rails3-generators'
gem "rspec", '>= 2.0.0.beta.19'
gem "rspec-rails", '>= 2.0.0.beta.19'
end
group :test do
gem "faker"
gem "rspec", '>= 2.0.0.beta.19'
gem "rspec-rails", '>= 2.0.0.beta.19'
gem "machinist", '>= 2.0.0.beta1'
gem "mocha"
gem "capybara", ">= 0.3.9"
end
And in spec/spec_helper.rb:
RSpec.configure do |config|
config.mock_with :mocha
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
end
Still, when I use the Rails generator...
rails generate scaffold foo name:string
...I get the following in spec/controllers/foos_controller_spec.rb:
def mock_foo(stubs={})
#mock_foo ||= mock_model(Foo, stubs).as_null_object
end
...which of course causes all specs to fail.
Does anyone know what I'm missing?
Thanks in advance.
In application.rb you'll need something like the following:
config.generators do |g|
g.test_framework :rspec
end
Further information available here:
http://guides.rubyonrails.org/generators.html#customizing-your-workflow
Related
I am attempting to run a code coverage report using deepcover's simplecov takeover and one test will always fail with a useless stack trace. Even if I skip the failing test with xit the following test will fail with the same error. Stack Trace
Gemfile:
gem 'aws-sdk-s3', '1.4.0' # Amazon WS
gem 'builder'
gem 'bundler'
gem 'fastercsv'
gem 'httpclient'
gem 'jbuilder', '~> 2.10'
gem 'json'
gem 'march_hare', '3.0.0', platform: :jruby # AMQP
gem 'multi_json', '1.9.0'
gem 'newrelic_rpm'
gem 'nokogiri', platform: :jruby
gem 'puma', '~> 4.3' # Known issue with puma 5 on ARM64 / Docker: https://github.com/jruby/jruby/issues/6821
gem 'rack'
gem 'sinatra', '~> 2.1'
gem 'tilt'
gem 'tilt-jbuilder', '>= 0.7', require: 'sinatra/jbuilder'
# gem 'jruby-rack' Is this needed??
gem 'tzinfo', '1.2.3'
gem 'uuidtools'
# Redis
gem 'hiredis'
gem 'redis', '~>3.2'
gem 'redlock'
group :development, :test do
gem 'pry'
gem 'pry-debugger-jruby'
gem 'pry-remote'
gem 'racksh'
gem 'rake'
gem 'rubocop', require: false
gem 'rubocop-rake', require: false
gem 'rubocop-rspec', require: false
gem 'rubocop-sequel', require: false
end
group :test do
# NOTE: latest v2 (v2.0.1 at the time of this writing) does not clean db between examples
gem 'database_cleaner-sequel', '~> 1'
gem 'coverband'
gem 'equivalent-xml', require: false # require after `rspec/matchers` in spec_helper.rb
gem 'fakeredis', require: 'fakeredis/rspec'
gem 'json_spec'
gem 'rack-test', require: 'rack/test'
gem 'rspec'
gem 'rspec-core'
gem 'rspec-expectations', require: 'rspec/expectations'
gem 'rspec_junit_formatter'
gem 'rspec-mocks'
gem 'simplecov', require: false
gem 'simplecov-cobertura'
gem 'vcr'
gem 'webmock'
end
I am running rspec and encountered the following error. I already looked for the similar posts but couldn't find the one that is relevant in this case.
'require': cannot load such file -- rspec/core/formatters/base_formatter (LoadError)
Ruby version: 2.4.3
Here is my Gemfile:
group :test do
gem 'database_cleaner', '~> 1.5'
gem 'factory_girl'
gem 'rack-test', require: 'rack/test'
gem 'rspec', '~> 3.5'
gem 'simplecov'
gem 'webmock'
gem 'timecop'
end
Here is the actual command:
/Users/<user_name>/.rbenv/versions/2.4.3/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /Users/<user_name>/.rbenv/versions/2.4.3/bin/rspec _3.7.1_ /Users/<user_name>/Desktop/github/test_rspec.rb --require teamcity/spec/runner/formatter/teamcity/formatter --format Spec::Runner::Formatter::TeamcityFormatter
test_rspec.rb:
require 'spec_helper'
describe 'tests' do
include_context 'scenario'
# sample spec
end
I had a similar issue, so:
I removed my Gemfile.lock
re run bundle install
I'm using spork to test a Sinatra application and with Ruby 1.9.2 the tests run in about 3.5 seconds but in Ruby 1.8.7 they average 1.2 seconds. I did try Ruby 1.9.3 and even JRuby but they had some errors with the gems I'm using. Is there a way to bring Ruby 1.9.2's rspec performance up to 1.8.7's level?
My Gemfile:
source :rubygems
gem 'sinatra', '1.3.1'
gem 'thin', '1.3.1'
gem 'haml', '3.1.4'
gem 'datamapper', '1.2.0'
gem 'dm-postgres-adapter', '1.2.0'
gem 'carrierwave', '0.5.8'
gem 'carrierwave-datamapper', '0.2.0'
group :test do
gem "dm-sqlite-adapter"
gem "spork"
gem "rspec"
gem "rack-test"
end
spec_helper.rb:
require 'rubygems'
require 'spork'
require 'sinatra'
require 'rack/test'
require 'rspec'
require File.join(File.dirname(__FILE__), '..', 'app.rb')
require File.join(File.dirname(__FILE__), '..', 'model/model.rb')
Spork.prefork do
set :environment, :test
set :files, "test_files"
end
Spork.each_run do
RSpec.configure do |config|
config.before(:each) { DataMapper.auto_migrate! }
config.after(:all) do
FileUtils.rm_rf(Dir["#{settings.root}/public/test_files"])
end
end
end
thanks!
There was a problem in the way ruby 1.9.2 require'd files during startup:
http://rhnh.net/2011/05/28/speeding-up-rails-startup-time
1.9.3 has a partial fix for this IIRC.
Not according to rspec's own test: https://gist.github.com/939865. It is supposed to be faster. It could be something slower in your stack.
I'm working through the upgrade to 3.1, and hit a snag.
I have a model (using Mongoid) that is having it's 'link' method overwritten by Rake, apparently. I'm getting this message:
WARNING: Global access to Rake DSL methods is deprecated. Please include
... Rake::DSL into classes and modules which use the Rake DSL methods.
WARNING: DSL method Profile#link called at /Users/jeffdeville/Documents/code/ruby/wg/app/models/profile.rb:62:in `block in eql?'
Because the method is resolving, I have no stacktrace or anything. This happens in my rspecs, but the problem does not happen in the console.
Gemfile is below.
Even if you don't know the answer, if you have any suggestions on how I'd even approach the debugging process here, that'd be fantastic.
-Jeff
source 'http://rubygems.org'
gem 'rails', "3.1.0"
gem 'rake'
gem 'mail'
gem 'compass', "~> 0.12.alpha.0"
gem 'jquery-rails'
gem 'haml-rails'
gem "html5-boilerplate"
gem 'mini_fb', :git => 'git://github.com/jeffdeville/mini_fb.git'
gem "json"
gem "bson"
gem "bson_ext"
gem 'sucker'
gem 'crack', :git => 'git://github.com/ericgj/crack.git'
gem 'hoptoad_notifier'
gem 'httparty'
gem 'facebooker2'
gem 'delayed_job'
gem 'delayed_job_mongoid'
gem 'unicorn'
gem 'hirefireapp'
gem 'mogli', :git => 'https://github.com/jeffdeville/mogli.git'
gem 'typhoeus'
gem 'koala'
gem 'foreman'
gem 'heroku'
gem 'rbing'
gem 'rmagick'
gem 'mini_magick'
gem 'aws-sdk'
gem 'mechanize'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', " ~> 3.1.0"
gem 'coffee-rails', "~> 3.1.0"
gem 'uglifier'
end
group :development, :test do
gem 'awesome_print'
gem 'pry'
end
group :development do
gem 'rails3-generators'
gem 'coffee-script'
gem 'guard'
gem 'guard-coffeescript'
gem 'guard-livereload'
gem 'guard-rspec'
gem 'guard-spork'
gem 'guard-bundler'
gem 'mailcatcher'
gem 'foreman'
end
group :test do
gem "factory_girl_rails"
gem 'fuubar'
gem 'spork', '~> 0.9.0.rc'
gem 'ruby-debug19'
gem "rspec"
gem "rspec-given"
gem "rspec-rails"
gem 'mocha'
gem "bourne"
gem 'webmock'
gem 'vcr'
gem 'jasmine'
gem 'email_spec'
gem 'timecop'
end
group :mac do
# if RUBY_PLATFORM =~ /darwin/i
gem 'rb-fsevent'
gem 'growl'
# end
end
I was having a similar problem when I did the following:
api_client = API::Client
api_model = 'task' #this is dynamic in reality...
api_client.send(api_model).list
Rake's Global DSL task method was clobbering the send for some reason but the call worked if I called it directly (without using send). In the end I just changed the dynamic call to an veal.
eval "api_client.#{api_model}.list"
It's slower but I'll change it back when they eventually remove the Global DSL from Rake.
HTH, YMMV ;-)
I'm sure that I am just overlooking something simple here but this has been driving me crazy all night! When trying to deploy a Rails 3.1.rc4 application to the Cedar stack on Heroku (I did this successfully a month ago with a similar Gemfile) I am receiving this error:
Could not find sprockets-2.0.0.beta.10 in any of the sources
My Gemfile looks like this:
source 'http://rubygems.org'
# Core
gem 'rails', '3.1.0.rc4'
# Asset template engines
gem 'sass-rails', "~> 3.1.0.rc"
gem 'coffee-script'
gem 'uglifier'
# Misc
gem 'devise'
gem 'jquery-rails'
gem 'omniauth'
gem 'fb_graph'
gem 'compass', git: 'https://github.com/chriseppstein/compass.git', branch: 'rails31'
gem 'haml'
gem 'cancan'
gem 'kaminari'
gem 'friendly_id', '~> 3.3.0', git: 'https://github.com/norman/friendly_id.git'
gem 'recaptcha', :require => 'recaptcha/rails'
gem 'aws-ses', '~> 0.4.3', :require => 'aws/ses'
# Local Environment
group :test do
# Pretty printed test output
gem 'turn', :require => false
gem 'sqlite3'
end
# Heroku Environment
group :production do
gem 'pg'
gem 'execjs'
gem 'therubyracer'
end
After searching around and finding this article on Google Groups, I determined that this must be fixable by adding this line
gem 'sprockets', '2.0.0.beta10'
to my Gemfile and then running
bundle update sprockets
This failed with
Could not find gem 'sprockets (= 2.0.0.beta10, runtime)' in any of the gem sources listed in your Gemfile.
and at this point I don't know what to do or how to handle this. Is it possible that I need to upgrade to Rails 3.1.rc5 and if so how may I do that without starting over from scratch?
Thank you for any help that you can provide!
-Robert
Just bump your rails version up to rc5
gem 'rails', '3.1.0rc5'
then:
bundle update