First try to make a controller test with RSpe

Hello, I’m trying to create a controller test with RSpec but I get the
error when I run

spec login_controller_spec.rb --format specdoc

…/…/app/controllers/application.rb:4: uninitialized constant
ActionController (NameError)
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require' from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:inrequire’
from ./login_controller_spec.rb:9
from
/usr/lib/ruby/gems/1.8/gems/rspec-1.1.4/lib/spec/runner/example_group_runner.rb:14:in
load' from /usr/lib/ruby/gems/1.8/gems/rspec-1.1.4/lib/spec/runner/example_group_runner.rb:14:inload_files’
from
/usr/lib/ruby/gems/1.8/gems/rspec-1.1.4/lib/spec/runner/example_group_runner.rb:13:in
each' from /usr/lib/ruby/gems/1.8/gems/rspec-1.1.4/lib/spec/runner/example_group_runner.rb:13:inload_files’
from
/usr/lib/ruby/gems/1.8/gems/rspec-1.1.4/lib/spec/runner/options.rb:98:in
run_examples' from /usr/lib/ruby/gems/1.8/gems/rspec-1.1.4/lib/spec/runner/command_line.rb:19:inrun’
from /usr/lib/ruby/gems/1.8/gems/rspec-1.1.4/bin/spec:4
from /usr/bin/spec:19:in `load’
from /usr/bin/spec:19

Here is my login_controller_spec :

require File.dirname(FILE) + ‘/…/spec_helper’

describe “A LoginController” do

before(:each) do
@login_controlleur = LoginController.new
end

it “should be valid” do
@login_controlleur.should be_valid
end
end

Here is my spec_helper :

require “rubygems”
require “activerecord”
require “spec”
LIB_DIR = File.join(File.expand_path(File.dirname(FILE)), *%w[… app
controllers])

Until now, I was working with Test::Unit, and all my test were
successfull
on my login_controller.rb file.
The file application.rb exists.
I have tried to add the line :

require “actioncontroller”

in my spec_helper, but I only get the error :

/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require': no such file to load -- ActionController (MissingSourceFile) from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:inrequire’
from
/usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:509:in
require' from /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:354:innew_constants_in’
from
/usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:509:in
require' from ./../spec_helper.rb:3 from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:ingem_original_require’
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
require' from ./login_controller_spec.rb:1 from /usr/lib/ruby/gems/1.8/gems/rspec-1.1.4/lib/spec/runner/example_group_runner.rb:14:inload’
from
/usr/lib/ruby/gems/1.8/gems/rspec-1.1.4/lib/spec/runner/example_group_runner.rb:14:in
load_files' from /usr/lib/ruby/gems/1.8/gems/rspec-1.1.4/lib/spec/runner/example_group_runner.rb:13:ineach’
from
/usr/lib/ruby/gems/1.8/gems/rspec-1.1.4/lib/spec/runner/example_group_runner.rb:13:in
load_files' from /usr/lib/ruby/gems/1.8/gems/rspec-1.1.4/lib/spec/runner/options.rb:98:inrun_examples’
from
/usr/lib/ruby/gems/1.8/gems/rspec-1.1.4/lib/spec/runner/command_line.rb:19:in
run' from /usr/lib/ruby/gems/1.8/gems/rspec-1.1.4/bin/spec:4 from /usr/bin/spec:19:inload’
from /usr/bin/spec:19

Could somebody help me find a solution to make my test working ?
Thanks
Thibault

Hello,

Try including your rails config/environment.rb file in your spec_helper:

spec_helper.rb

ENV[“RAILS_ENV”] = “test”
require “/…/config/environment”


Joseph W.
http://www.joesniff.co.uk

Bug fixed ! thank you very much !

Hello, I got an error when I run my spec test of a controlleur : the get
and post methodes are unknown. here is my test file :

setup_controller_spec.rb :

require File.dirname(FILE) + ‘/…/spec_helper’
require “#{LIB_DIR}/application”
require “#{LIB_DIR}/setups_controller”

describe “A SetupController” do
before :each do
@setup_controller = SetupsController.new
end

it “should get step 1 first” do
get ‘step1’
response.should be_success
end

here is my spec_helper file :

ENV[“RAILS_ENV”] = “test”
require “rubygems”
require “activerecord”
require “spec”
require File.dirname(FILE) + ‘/…/config/environment’
LIB_DIR = File.join(File.expand_path(File.dirname(FILE)), *%w[… app
controllers])

puts LIB_DIR

I get the error :

NoMethodError in ‘A SetupController should get step 1 first’
undefined method `get’ for
#Spec::Example::ExampleGroup::Subclass_1:0xb786cefc
./setup_controller_spec.rb:13:

(the same thing happens with the post methode)

depending on the RSpec doc, this methode should exist in the rspec
plugin…
http://rspec.info/documentation/rails/writing/controllers.html
##sample :
get ‘some_action’
response.should render_template(“path/to/template/for/action”)

I think it’s another require problem…
Or maybe I make a mistake in my installation of the plugin
(I just execute the 2 commands in my working directory :
ruby script/plugin install
http://rspec.rubyforge.org/svn/tags/REL_1_1_3/rspec
ruby script/plugin install
http://rspec.rubyforge.org/svn/tags/REL_1_1_3/rspec_on_rails
)

Could somebody help me for this problem ?
Thanks
Thibault

On Thu, Aug 14, 2008 at 12:42 PM, [email protected] wrote:

require “#{LIB_DIR}/setups_controller”

puts LIB_DIR

I get the error :

NoMethodError in ‘A SetupController should get step 1 first’
undefined method `get’ for
#Spec::Example::ExampleGroup::Subclass_1:0xb786cefc
./setup_controller_spec.rb:13:

The spec doesn’t know it is supposed to be a controller spec. For it
to know that, it needs to be in RAILS_ROOT/spec/controllers, or you
can use this argument to the describe method:

describe SetupController, :type => :controller do

end

HTH,
David

and, is it SetupController or SetupsController ?

I have checked : I am in the RAILS_ROOT/spec/controllers directory
I have tried with the second solution, but the get and post methodes
remain undefined.
Could it be a version compatibility problem ?
I’m using rails 2.1.0, and the REL_1_1_3 for the rspec plugin.
What is the best version of rspec and rails to make them work well
together ?
Thib.

On Aug 14, 2008, at 1:12 PM, [email protected] wrote:

I have checked : I am in the RAILS_ROOT/spec/controllers directory
I have tried with the second solution, but the get and post methodes
remain undefined.
Could it be a version compatibility problem ?
I’m using rails 2.1.0, and the REL_1_1_3 for the rspec plugin.
What is the best version of rspec and rails to make them work well
together ?

Rails 2.1.0 came out months after Rspec 1.1.3 :slight_smile:

Try Rspec 1.1.4 (at github),

Cheers,
David

I have updated the plugins, and it seems to be working well indeed !
Thanks
Thib

and, is it SetupController or SetupsController ?

it’s a SetupsController.
my controller file is RAILS_ROOT/app/controllers/setups_controller.rb

I have just tried with a setups_controller_spec.rb spec file, but the
undefined method remains

here is my new setups_controller_spec.rb file :
in RAILS_ROOT/spec/controllers

quire File.dirname(FILE) + ‘/…/spec_helper’
require “#{LIB_DIR}/application”
require “#{LIB_DIR}/setups_controller”

describe SetupsController, :type => :controller do
before :each do
@setup_controller = SetupsController.new
end
it “should exist” do
@setup_controller.should_not be_nil
@setup_controller.should be_an_instance_of(SetupsController)
end

it “should get step 1 first” do
get ‘step1’
response.should be_success
end
end