【问题标题】:Suddenly, every test in our Rails 4 app fails突然,我们的 Rails 4 应用程序中的每个测试都失败了
【发布时间】:2015-11-13 23:12:48
【问题描述】:

我的同事告诉我,这些测试在运行时通过了。但是我刚刚从 Bitbucket 中检查了代码,对我来说,测试失败了。我确保我的机器与我的同事具有相同的版本:

Rails 4.2.4

Ruby 2.2.3

rake,版本 10.4.2

在经历了一些 nokogiri 的痛苦之后,我能够让“捆绑安装”运行。

但如果我这样做:

   bin/rake test

每次测试都失败:

27 次运行,0 次断言,0 次失败,27 次错误,0 次跳过

出现如下错误:

    6) Error:
    ProfilesControllerTest#test_#update:
    NoMethodError: undefined method `type' for nil:NilClass


    Error:
    ProfilesControllerTest#test_#update:
    NoMethodError: undefined method `each' for nil:NilClass

以下是一些失败代码的示例:

    require 'test_helper'

    class ProfilesControllerTest < ActionController::TestCase

      test '#update' do
        profile = profiles(:no_address_profile)
        login(profile)
        VCR.use_cassette('essex_street_ny') do
          patch :update, profile: {address: '15 Essex street, New York, NY, USA', zipcode: '10002'}
        end
        updated_profile = Profile.find_by_user_id(profile.user_id)
        assert_equal '15 Essex street, New York, NY, USA', updated_profile.address
        assert_equal '10002', updated_profile.zipcode
        assert_redirected_to root_path
      end

      test '#update with only zipcode' do
        profile = profiles(:no_address_profile)
        login(profile)
        VCR.use_cassette('only_10002_zipcode') do
          patch :update, profile: {zipcode: '10002'}
        end
        updated_profile = Profile.find_by_user_id(profile.user_id)
        assert_equal '10002', updated_profile.zipcode
        assert_redirected_to root_path
      end

      def login(profile)
        session[:user_id] = profile.user_id
      end
    end

那么这里到底发生了什么?

更新:

如果我这样做:

     rake db:fixtures:load

我明白了:

   rake aborted!
   NoMethodError: undefined method `type' for nil:NilClass

【问题讨论】:

    标签: ruby-on-rails-4 rake


    【解决方案1】:

    听起来您需要在测试环境中运行迁移。你可以这样做:

    rake db:migrate RAILS_ENV=test
    

    【讨论】:

      猜你喜欢
      • 2013-12-10
      • 1970-01-01
      • 2021-04-10
      • 1970-01-01
      • 2011-01-05
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多