【问题标题】:Rspec failure when it should be passing应该通过的Rspec失败
【发布时间】:2011-02-26 19:30:51
【问题描述】:

当我运行bundle exec rspec spec/ 时,我的一个测试失败了,应该通过了。这是错误:

Failure/Error: @user = Factory(:user)
     NoMethodError:
       undefined method `Factory' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_2:0x1037c0a70>
     # ./spec/controllers/users_controller_spec.rb:21

这是测试代码:

  describe "GET 'show'" do

  before(:each) do
    @user = Factory(:user)
  end

  it "should be successful" do
    get :show, :id => @user
    response.should be_success
  end

  it "should find the right user" do
    get :show, :id => @user
    assigns(:user).should == @user
  end
end

我安装了 Factory Girl gem,所以我不确定是什么问题。

【问题讨论】:

  • 您的Gemfile 中是否包含工厂女孩?

标签: ruby-on-rails rspec gem factory-bot


【解决方案1】:

您说您安装了factory_girl,但它是否在您的GemfileGemfile.lock 文件中? (您可能需要bundle install 才能将其放入Gemfile.lock)。由于您使用的是 bundle exec,bundler 将检查您的 Gemfile.lock 是否应该加载它。

[编辑]

转念一想,这看起来像是duplicate of this SO question

【讨论】:

  • 是的,我最初运行bundle install,我在Gemfile.lock 文件中看到了工厂女孩。不要以为就是这样。
  • 我只有在group :test do 块中有gem 'factory_girl_rails', '1.0'。我也不认为是这样。
  • 最后一个建议:查看link in my edited answer
  • 哦,没关系,这很愚蠢。我只需要重新启动 spork :/
  • 无论如何我都会给你,因为你基本上是对的,这是一个重复的问题。
【解决方案2】:

可能不是这样,但是您是否尝试过重启网络服务器?

【讨论】:

    【解决方案3】:

    这里的答案对我没有帮助。我发现解决我的问题的方法是在 spec_helper.rb 中添加一行

    require 'factory_girl'
    RSpec.configure do |config|
      config.include FactoryGirl::Syntax::Methods
    end
    

    然后在我的规范中我会这样称呼它

    person = build(:person)
    

    希望对某人有所帮助。找了大约 3 个小时。

    【讨论】:

      【解决方案4】:

      我认为,这是一个正确的解决方案:

        @user = Factory(:user)
      

      替换为

        @user = Factory.create(:user)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-14
        • 2015-07-08
        • 1970-01-01
        相关资源
        最近更新 更多