RSpec and testing Rails Gem - testing

I'm trying to set a testing framework for a rails engine.
I have everything working, I generated the corresponding rspec file with:
rails g rspec:install
However everytime I want to add a rspec testing file with:
rails g integration_test blah
The RSpec doesn't take over and instead the standard unit testing framework of rails creates the corresponding files. Any thoughts on where the problem might be?

I solved that problem running rails g with --integration-tools option
rails g integration_test blah --integration-tools=rspec
I do not know how to set rspec as default tool.

I always add in my application.rb :
config.generators do |g|
g.test_framework :rspec
end
That allows RSpec to replace Rails generator for tests.

you should put in application.rb:
config.generators do |g|
g.integration_tool :rspec
g.test_framework :rspec
end
source: rails configuration guide

Related

acts_as_commentable_with_threading uninitialized constant Post::Comment Rails 3.1.3

I have upgraded my application from Rails 2.3.5 to Rails 3.1.3. I had acts_as_commentable_with_threading and awesome_nested_set as plugins. Now I have added in GemFile for Rails 3.1.3 as below
gem 'awesome_nested_set'
gem 'acts_as_commentable_with_threading'
I have code in the model as below
class Post < ActiveRecord::Base
acts_as_commentable
end
The migration for acts_as_commentable_with_threading is already applied in the database.
I have statement in the view as
<%= pluralize(post.root_comments.size, "comment") %> on this post
When I try to load that view I get error at that line as
uninitialized constant Post::Comment
What can be the problem. Please help me.
Thanks in advance!
You might have forgot to run the generator and the migration afterwards:
rails generate acts_as_commentable_with_threading_migration
or
rails generate acts_as_commentable_upgrade_migration
and after:
bin/rake db:migrate
It's first steps of github repo instructions

no such file to load -- action_controller/integration - NoMethodError for Rails 3 and Webrat

I'm getting the following failure during RSpec tests..
no such file to load -- action_controller/integration
..using Rails 3, RSpec 2 and Webrat, if I include the Webrat helpers in the following way (the idea was to use Webrat for the have_tag and have_selector methods instead of assert_select).
RSpec.configure do |config|
..
config.include Webrat::HaveTagMatcher
end
Yet apparently Webrat and Rails 3 are not compatible yet. One solution is to avoid the Webrat gem and to use assert_select instead. Has anyone a better solution? How do you avoid the error?
I have not found a solution for this problem, only a work around. You can use the have_selector method of Capybara instead of Webrat, at least in RSpec request tests. There are certain difficulties with RSpec2 and Capybara (page.should have_selector only works if you use Capybara's 'visit' method, and not the RSpec method get '/some/path'), but basically it works.

Rails 3 + FactoryGirl: NameError: uninitialized constant Factory

ruby-1.9.2-p180 :007 > Factory.define :user do |user|
ruby-1.9.2-p180 :008 > user.email "user#example.com"
ruby-1.9.2-p180 :009?> user.password "foobar"
ruby-1.9.2-p180 :010?> user.password_confirmation "foobar"
ruby-1.9.2-p180 :011?> end
NameError: uninitialized constant Factory
My Gemfile:
group :test do
gem "rspec-rails"
gem 'webrat', '0.7.1'
gem 'spork', '0.9.0.rc4'
gem 'factory_girl_rails'
end
Even tough it seems I have everything as it should, I keep getting that error. I also have factories.rb created.
Thanks
I suppose you try in console in development environment. But you add the Factory gem only in test environment.
If you want access to Factory_girl in development use in your Gemfile :
group :test, :development do
gem 'factory_girl_rails'
end
Or if you want test your Factory launch your console in test environment :
rails c test
We had a similar problem on our end, rake spec seemed to be randomly failing with the error uninitialized constant FactoryGirl. The error was random -> coming and going. We went back half a dozen git commits to try and resolve it. In the end it was a silly mistake.
The fundamental problem is RAILS_ENV is set to development. It needs to be set to test when you run rake spec.
Address by:
Making certain that we are running rake spec in the RAILS_ENV test environment and its exported/sourced properly. To never confuse our environemnts, we modified zsh $RPROMPT env variable to show the current env.
export RPROMPT="[%{$fg_no_bold[yellow]%}$RAILS_ENV%{$reset_color%}]"
Require FactoryGirl in the spec ruby files gave a much better error message. At least rspec would run vs just fail outright this way when the environment was wrong. We also updated our gemfile to make sure factory_girl_rails and factory_girl were loaded both for development and testing.
Now we just run rpsec using the gem guard in a dedicated terminal with the proper RAILS_ENV set.
Its one of those gotchas.
We're running Ruby 1.9.3, Rails 3.2.9, Rspec 2.12, factory_girl 4.1.
I also ran into this while I was doing the Hartl tutorial.
If you are using "Spork" to speed up your tests and it is running in the background when you add the FactoryGirl code, you will need to stop it and restart it.
You should also change Factory.define to FactoryGirl.define, like in this example
FactoryGirl.define do
factory :user do
name 'John Doe'
date_of_birth { 21.years.ago }
end
end
from the factory_girl documentation

Create a rails 3 plugin with RSpec

The question explains itself...
I wanna test a new rails plugin with rspec, are there any specific generators, gems?
I used rails g plugin blabla and I got an error on rspec generator: error rspec [not found].
FYI - there is an open github issue about this: https://github.com/rspec/rspec-rails/issues/#issue/258.

Rails 3 and vlad the deployer

i couldn't find anywhere if vlad it's compatible with Rails 3, but i tried to deploy a new rails 3 app that we're developing and always returns the same error:
Error loading vlad: no such file to load -- vladrake aborted!
Don't know how to build task 'vlad:init_setup:production'
I'm using ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-darwin9.8.0]
And rails 3.0.1 and i testit with vlad 2.0.0 and vlad 2.1.0
In the Rakefile i have this:
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
require 'rake'
begin
require 'rubygems'
require 'vlad'
Vlad.load :scm => :git, :app => :passenger
rescue LoadError => e
$stderr << "Error loading vlad: #{e}"
end
If anyone has an idea where to look at it in order to fixit or maybe tell me if vlad it's rails 3 ready?
Thanks in advance for your time
Cavi
Maybe you already figured this out, but do you have vlad included in your Gemfile? You probably want to add:
group :development do
gem 'vlad-git'
gem 'vlad'
end
Then run bundle.