railstutorial.org - 6.11 - rspec失败,是postgresql的问题吗?

时间:2022-11-20 00:15:31

I am going through the rails tutorial and am at section 6.11

我正在阅读rails指南,并在第6.11节

For some reason, I am getting 4 examples and 4 failures in rspec vs 4 examples and 1 failure (and consequently 0) as in the tutorial.

出于某种原因,我在rspec中获得4个示例和4个失败,而在本教程中有4个示例和1个失败(因此为0)。

I am seeing this in my rspec:

我在我的rspec中看到了这个:

Failure/Error: @user = User.new(name: "Example User", email: "user@example.com")
     ActiveRecord::StatementInvalid:
       PGError: ERROR:  relation "users" does not exist

The thing is, the tutorial is going along in sqlite, but I migrated to postgres (as suggested in the tutorial earlier) so I could learn about postgres as well. Is this what is causing the problems? Shouldn't activerecord be making it transparent as to what kind of database I am using anyways?

问题是,教程是在sqlite中进行的,但我迁移到postgres(如前面教程中所建议的那样),所以我也可以了解postgres。这是导致问题的原因吗?对于我正在使用的数据库,不应该使activerecord变得透明吗?

I have created the proper databases, updated my database.yml, run a db:mgirate and the command

我创建了正确的数据库,更新了我的database.yml,运行db:mgirate和命令

User.new(name: "Example User", email: "user@example.com")

works fine in a sandboxed console.

在沙盒控制台中正常工作。

Any help greatly appreciated. I am trying to stay on top of rspec here, to me it is the most challenging part about learning Rails. It almost feels like one spends 90% of one's energy writing and debugging tests instead. (The only stumbling blocks/mind bending headca I've encountered so far have been rspec tests)

任何帮助非常感谢。我想在这里保持对rspec的顶端,对我而言,这是学习Rails最具挑战性的部分。这几乎感觉就像一个人花费了90%的能量写作和调试测试。 (到目前为止我遇到的唯一绊脚石/心灵弯曲头部是rspec测试)

The user_rspec.rb:

require 'spec_helper'

describe User do

  before do
    @user = User.new(name: "Example User", email: "user@example.com")
  end

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }

  it { should be_valid }

  describe "when name is not present" do
    before { @user.name = " " }
    it { should_not be_valid }
  end
end

--

As a secondary question, I find that my rspec tests are running multiple times? I installed the ruby-gntp gem and am finding growl popping up multiple times each time a test is run, with the same information.

作为第二个问题,我发现我的rspec测试运行了多次?我安装了ruby-gntp gem,并且每次运行测试时都会发现咆哮声弹出多次,信息相同。

railstutorial.org  -  6.11  -  rspec失败,是postgresql的问题吗?

2 个解决方案

#1


3  

OK, I figured it out.

好的,我明白了。

The problem was that the development database was not being migrated to the test database, even when running rake db:migrate, rake db:rollback then migrate, rake db:reset.

问题是开发数据库没有迁移到测试数据库,即使在运行rake db:migrate,rake db:rollback then migrate,rake db:reset时也是如此。

The solution was to clone the database:

解决方案是克隆数据库:

bundle exec rake db:test:prepare

#2


0  

I had the same circumstances - migrating from SQLite to PG. After a reinstall of PG - I started getting 20 rspec failures - all around Users. After hours of frustration - one command fixed it all:

我有相同的情况 - 从SQLite迁移到PG。重新安装PG之后 - 我开始收到20个rspec失败 - 所有用户都在。经过几个小时的挫折 - 一个命令修复了所有:

rake db:test:prepare or bundle exec rake db:test:prepare (depending on your settings)

rake db:test:准备或捆绑exec rake db:test:prepare(取决于你的设置)

Ruby Guides - mentions it too - http://guides.rubyonrails.org/testing.html

Ruby指南 - 也提到它 - http://guides.rubyonrails.org/testing.html

When you do end up destroying your testing database (and it will happen, trust me), you can rebuild it from scratch according to the specs defined in the development database. You can do this by running rake db:test:prepare.

当你最终破坏你的测试数据库时(它会发生,相信我),你可以根据开发数据库中定义的规范从头开始重建它。您可以通过运行rake db:test:prepare来完成此操作。

#1


3  

OK, I figured it out.

好的,我明白了。

The problem was that the development database was not being migrated to the test database, even when running rake db:migrate, rake db:rollback then migrate, rake db:reset.

问题是开发数据库没有迁移到测试数据库,即使在运行rake db:migrate,rake db:rollback then migrate,rake db:reset时也是如此。

The solution was to clone the database:

解决方案是克隆数据库:

bundle exec rake db:test:prepare

#2


0  

I had the same circumstances - migrating from SQLite to PG. After a reinstall of PG - I started getting 20 rspec failures - all around Users. After hours of frustration - one command fixed it all:

我有相同的情况 - 从SQLite迁移到PG。重新安装PG之后 - 我开始收到20个rspec失败 - 所有用户都在。经过几个小时的挫折 - 一个命令修复了所有:

rake db:test:prepare or bundle exec rake db:test:prepare (depending on your settings)

rake db:test:准备或捆绑exec rake db:test:prepare(取决于你的设置)

Ruby Guides - mentions it too - http://guides.rubyonrails.org/testing.html

Ruby指南 - 也提到它 - http://guides.rubyonrails.org/testing.html

When you do end up destroying your testing database (and it will happen, trust me), you can rebuild it from scratch according to the specs defined in the development database. You can do this by running rake db:test:prepare.

当你最终破坏你的测试数据库时(它会发生,相信我),你可以根据开发数据库中定义的规范从头开始重建它。您可以通过运行rake db:test:prepare来完成此操作。