我正在尝试使用bin rails\test
开始构建并运行我的rspec测试。他们过去曾经工作过,但是现在当我运行它们时,每次测试都会得到以下错误。
Error:
VenueTest#test_should_not_save_venue_without_name:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "application" does not exist
LINE 1: DELETE FROM "application"
^
: DELETE FROM "application"
Finished in 0.382377s, 41.8435 runs/s, 0.0000 assertions/s.
16 runs, 0 assertions, 0 failures, 16 errors, 0 skips
我几乎没有写任何测试,所以它只是现在失败的预卷。
我已经尝试运行迁移(运行正常)并且确实创建了测试数据库。我也试过了:
rake db:test:prepare
rake db:test:load
但仍然得到同样的错误。我也没有名为'application'的模型,所以不知道为什么它试图从“应用程序”中删除?
我猜这是某种奇怪的配置,我一路上搞砸了,但不知道它会是什么!
所有测试都失败了,但有问题的测试是:
Home_Controller_test.rb
require 'test_helper'
class HomeControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
get home_index_url
assert_response :success
end
end
offers_controller_test.rb
require 'test_helper'
class OffersControllerTest < ActionDispatch::IntegrationTest
setup do
@offer = offers(:one)
end
test "should get index" do
get offers_url
assert_response :success
end
test "should get new" do
get new_offer_url
assert_response :success
end
test "should create offer" do
assert_difference('Offer.count') do
post offers_url, params: { offer: { desc: @offer.desc, end: @offer.end, offertype: @offer.offertype, start: @offer.start } }
end
assert_redirected_to offer_url(Offer.last)
end
test "should show offer" do
get offer_url(@offer)
assert_response :success
end
test "should get edit" do
get edit_offer_url(@offer)
assert_response :success
end
test "should update offer" do
patch offer_url(@offer), params: { offer: { desc: @offer.desc, end: @offer.end, offertype: @offer.offertype, start: @offer.start } }
assert_redirected_to offer_url(@offer)
end
test "should destroy offer" do
assert_difference('Offer.count', -1) do
delete offer_url(@offer)
end
assert_redirected_to offers_url
end
end
venues_controller_test.rb
require 'test_helper'
class VenuesControllerTest < ActionDispatch::IntegrationTest
setup do
@venue = venues(:one)
end
test "should get index" do
get venues_url
assert_response :success
end
test "should get new" do
get new_venue_url
assert_response :success
end
test "should create venue" do
assert_difference('Venue.count') do
post venues_url, params: { venue: { desc: @venue.desc, exists: @venue.exists, latitude: @venue.latitude, longitude: @venue.longitude, name: @venue.name, region: @venue.region, vtype: @venue.vtype } }
end
assert_redirected_to venue_url(Venue.last)
end
test "should show venue" do
get venue_url(@venue)
assert_response :success
end
test "should get edit" do
get edit_venue_url(@venue)
assert_response :success
end
test "should update venue" do
patch venue_url(@venue), params: { venue: { desc: @venue.desc, exists: @venue.exists, latitude: @venue.latitude, longitude: @venue.longitude, name: @venue.name, region: @venue.region, vtype: @venue.vtype } }
assert_redirected_to venue_url(@venue)
end
test "should destroy venue" do
assert_difference('Venue.count', -1) do
delete venue_url(@venue)
end
assert_redirected_to venues_url
end
end
修改
根据要求在下面进行Stacktrace:
bin / rails test test / controllers / offers_controller_test.rb:18
E
Error:
VenueTest#test_should_not_save_venue_without_name:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "application" does not exist
LINE 1: DELETE FROM "application"
^
: DELETE FROM "application"
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:614:in `async_exec'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:614:in `block (2 levels) in exec_no_cache'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies/interlock.rb:46:in `block in permit_concurrent_loads'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/concurrency/share_lock.rb:185:in `yield_shares'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies/interlock.rb:45:in `permit_concurrent_loads'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:613:in `block in exec_no_cache'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:612:in `block (2 levels) in log'
/Users/James/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:611:in `block in log'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:603:in `log'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:612:in `exec_no_cache'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:599:in `execute_and_clear'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:92:in `exec_delete'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:145:in `delete'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/query_cache.rb:17:in `delete'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/fixtures.rb:546:in `block (4 levels) in create_fixtures'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/fixtures.rb:544:in `each_key'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/fixtures.rb:544:in `block (3 levels) in create_fixtures'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/fixtures.rb:540:in `each'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/fixtures.rb:540:in `block (2 levels) in create_fixtures'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `block in transaction'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/transaction.rb:194:in `block in within_new_transaction'
/Users/James/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/transaction.rb:191:in `within_new_transaction'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `transaction'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/fixtures.rb:538:in `block in create_fixtures'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql/referential_integrity.rb:22:in `disable_referential_integrity'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/fixtures.rb:523:in `create_fixtures'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/fixtures.rb:1028:in `load_fixtures'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/fixtures.rb:965:in `setup_fixtures'
/Users/James/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/fixtures.rb:851:in `before_setup'
编辑 - 添加了Venue_test.rb
require 'test_helper'
class VenueTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
test "should not save venue without name" do
venue = Venue.new
assert_not venue.save, "Saved the venue without a title"
end
end
答案 0 :(得分:0)
以防其他人看到此问题,test / fixtures文件夹中有额外的文件(可能由生成器创建)。 @dskecse确定了这个问题。
删除后,脚本运行正常。
答案 1 :(得分:0)
在以下命令中运行对我来说解决了该错误:
RAILS_ENV=test rake db:test:prepare
感谢@ guy-yogev