使用Rspec和Shoulda,为什么'setup'未定义?

时间:2012-06-11 16:43:29

标签: ruby-on-rails unit-testing rspec shoulda

更新2:require {path_to_spec_helper}解决了设置未定义的问题,但现在所有静态变量都突然未定义,并且所有FactoryGirl制作的对象都没有通过验证(即使检查显示对象应该通过验证) 。并且将FactoryGirl更改为save_with_validation(false)只会使我的测试中的对象为零,从而破坏所有内容。

更新: 我把它扔进了我的代码:

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

并且respond_to行打印为true,但随后继续抛出下面的method_missing错误。所以,我猜它在上下文中是未定义的?它不像那样。

原帖:

出于某种原因,我不知道,在我的测试中似乎未定义context / should / setup。我将所有设置更改为before(:each),然后shouldcontext会出现问题。当我将所有rspec / shoulda匹配器更改为describe - before(:each) - it{}的旧skool样式时,我的测试将会运行,但实际上不会执行。 (控制台中的进度显示没有运行测试(没有点))。

所以,我想,我如何验证我的测试环境是否设置正确?

这是我的配置
宝石文件:

# 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

测试肝中需要的东西:

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'

二进制信息
红宝石1.8.7
rvm 1.7.2
宝石1.8.21
捆绑1.1.4
rake 0.9.2.2
rails 2.3.14

我的错误

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

堆栈跟踪:

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'

堆栈上最后一个代码位的第257行的代码:

  context "some context" do
    setup do
    ...

1 个答案:

答案 0 :(得分:0)

如果没有,首先必须生成spec / spec_helper.rb和spec / rails_helper.rb文件。您可以使用以下命令执行此操作:

rails generate rspec:install

创建文件后,请确保rails_helper.rb需要spec_helper.rb,否则它将无效,require "rails_helper"位于您的规范文件之上。

您可以在此处查看rails_helper.rbspec_helper.rb的更详细配置:https://kolosek.com/rails-rspec-setup