我的同事告诉我这些测试在他们运行时通过。但我刚刚从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
答案 0 :(得分:0)
听起来您需要在测试环境中运行迁移。你可以这样做:
rake db:migrate RAILS_ENV=test