NullVoxPopuli
NullVoxPopuli

Reputation: 65143

Using Rspec and Shoulda, why is 'setup' undefined?

UPDATE 2: require {path_to_spec_helper} solves the setup undefined issue, but now all of the static variables are suddenly undefined, and All FactoryGirl-made objects don't pass validation (even though inspecting shows that the object should pass validation). And Changing FactoryGirl to save_with_validation(false) just makes the object nil in my tests, breaking everything.

UPDATE: I threw this into my code:

  context "some context" do
    ap self.respond_to?(:setup)
    setup do
        raise "stop"

And the respond_to line printed true, but then proceeded to throw the method_missing error below. So, I guess it's just undefined within context? It didn't used to be that way.

Original Post:

For some reason, unknown to me, it seems that context / should / setup are undefined in my tests. I'd change all the setup's to before(:each)'s and then there would be a problem with should or context. When I change all of the rspec / shoulda matchers to the old-skool style of describe - before(:each) - it{}, my tests will run, but won't actually get executed. (the progress in the console shows no tests being run (no dots)).

So, I guess, how do I verify my test environment is set up properly?

Here is my configuration
gem file:

# can't be in test group because it's required in the rake file
gem "single_test"# because running all the tests all the time takes too long

group :test do
    # helper gems
    gem "rspec-rails", "1.3.4"
    gem "rspec", "1.3.2"
    gem "shoulda"

    gem "database_cleaner"
    gem "crack" #for XML / JSON conversion

    gem "mocha" #required for .requires and .stubs
    gem "factory_girl", :require => false

    # view and functional
    gem "capybara", "1.1.1"
    gem "cucumber", "1.1.0"
    gem "cucumber-rails", "0.3.2"
    gem "culerity"

    gem "launchy"
    gem "hpricot"
    gem "gherkin"
    gem "rack"
    gem "webrat"

    # tools
    gem "json"
    gem "curb"

end

Required things in test hepler:

require File.expand_path(File.dirname(__FILE__) + "/../config/environment")

require "bundler/setup"
Bundler.require(:test)

require 'test_help'
require 'spec'
require 'spec/expectations'
require 'factory_girl'

binary info:
ruby 1.8.7
rvm 1.7.2
gem 1.8.21
bundle 1.1.4
rake 0.9.2.2
rails 2.3.14

And my error:

`method_missing': undefined method `setup' for Spec::Example::ExampleGroup::Subclass_1:Class (NoMethodError)

stack trace:

from test/unit/my_test.rb:257
from /Users/me/.rvm/gems/ruby-1.8.7-p352/gems/rspec-1.3.2/lib/spec/example/example_group_methods.rb:188:in `module_eval'
from /Users/me/.rvm/gems/ruby-1.8.7-p352/gems/rspec-1.3.2/lib/spec/example/example_group_methods.rb:188:in `subclass'
from /Users/me/.rvm/gems/ruby-1.8.7-p352/gems/rspec-1.3.2/lib/spec/example/example_group_methods.rb:55:in `describe'
from /Users/me/.rvm/gems/ruby-1.8.7-p352/gems/rspec-1.3.2/lib/spec/example/example_group_factory.rb:31:in `create_example_group'

code around line 257 of the last code bit on the stack:

  context "some context" do
    setup do
    ...

Upvotes: 1

Views: 783

Answers (1)

Nesha Zoric
Nesha Zoric

Reputation: 6620

If you didn't, first you have to generate your spec/spec_helper.rb and spec/rails_helper.rb files. you can do this with the following command:

rails generate rspec:install.

Once the files are created, make sure rails_helper.rb requires spec_helper.rb, otherwise it will not work and require "rails_helper" on top of your spec files.

You can check more detailed configuration of rails_helper.rb and spec_helper.rb here: https://kolosek.com/rails-rspec-setup

Upvotes: 0

Related Questions