[rspec-users] undefined method get

David Chelimsky dchelimsky at gmail.com
Sat Oct 31 10:43:43 EDT 2009


On Oct 29, 2009, at 6:04 PM, BrianTheCoder wrote:
> So here's the deal. Was using rack-test in rspec, cause I like passing
> paths instead of an action name, but it doesn't have access to the
> session, flash, cookies, etc. So I tried to switch to normal rspec and
> here's what I get (gist is working for me, so just pasting here)
>
> # error
>
> 1)
> NoMethodError in 'PagesController should get the home page'
> undefined method `get' for
> #<ActiveSupport::TestCase::Subclass_1:0x401648c>
> ./spec/functional/public/pages_controller_spec.rb:5:

For rspec to know that you're writing a controller spec and provide  
the services you're looking for (like the get() method), you need one  
of two conditions:

1. the file lives in the ./spec/controllers/ directory or any of its  
subdirectories

2. explicit declaration:

describe PagesController, :type => :controller do
   ...
end

HTH,
David

>
> # spec
>
> require File.join(File.dirname(__FILE__),'..','..','spec_helper')
>
> describe PagesController do
>  it "should get the home page" do
>    get 'home'
>    response.should be_redirect
>  end
> end
>
> # controller
>
> class PagesController < ApplicationController
>  def home
>    render :layout => false
>  end
> end
>
> # spec_helper.rb
>
> require 'rubygems'
> require 'spork'
>
> Spork.prefork do
>  # Loading more in this block will cause your tests to run faster.
> However,
>  # if you change any configuration or code from libraries loaded
> here, you'll
>  # need to restart spork for it take effect.
>  ENV["RAILS_ENV"] ||= 'test'
>  require File.dirname(__FILE__) + "/../config/environment"
>  require 'spec/autorun'
>  require 'spec/rails'
>  require 'faker'
>  require 'nokogiri'
>
>  Spec::Runner.configure do |config|
>    config.before(:all) do
>      DataMapper.auto_migrate!
>      ActionMailer::Base.delivery_method = :test
>      ActionMailer::Base.perform_deliveries = true
>      ActionMailer::Base.deliveries = []
>
>    end
>
>    config.before do
>      @time_now = Time.now
>      Time.stub!(:now).and_return(@time_now)
>
>      def emails; ActionMailer::Base.deliveries end
>
>      emails.clear
>
>      repository do |r|
>        transaction = DataMapper::Transaction.new(r)
>        transaction.begin
>        r.adapter.push_transaction(transaction)
>      end
>    end
>
>    config.after do
>      repository do |r|
>        adapter = r.adapter
>        while adapter.current_transaction
>          adapter.current_transaction.rollback
>          adapter.pop_transaction
>        end
>      end
>    end
>  end
>
> end
>
> Spork.each_run do
>  # This code will be run each time you run your specs.
>  Dir[File.dirname(__FILE__) + '/helpers/*.rb'].each{|helper| require
> helper}
>  require File.dirname(__FILE__) / 'fixtures'
> end
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Cheers,
David





More information about the rspec-users mailing list