【问题标题】:Using Rspec and Shoulda, why is 'setup' undefined?使用 Rspec 和 Shoulda,为什么“设置”未定义?
【发布时间】:2012-06-11 16:43:28
【问题描述】:

更新 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{} 的老式风格时,我的测试将运行,但实际上不会执行。 (控制台中的进度显示没有正在运行测试(无点)。

那么,我想,我该如何验证我的测试环境是否设置正确?

这是我的配置
宝石文件:

# 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

测试 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'

二进制信息
红宝石 1.8.7
rvm 1.7.2
宝石 1.8.21
捆绑 1.1.4
耙 0.9.2.2
轨道 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
    ...

【问题讨论】:

  • 你的规范文件顶部有require 'spec_helper'吗?
  • 我没有规格文件?或规范助手?所有这些测试都在 /test 目录中

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


【解决方案1】:

如果您不这样做,首先您必须生成您的 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    相关资源
    最近更新 更多