我正在阅读 Rails 教程,并在第 6.11 节
出于某种原因,我在 rspec 中得到了 4 个示例和 4 个失败,而在本教程中,我得到了 4 个示例和 1 个失败(因此为 0)。
我在我的 rspec 中看到了这一点:
Failure/Error: @user = User.new(name: "Example User", email: "user@example.com")
ActiveRecord::StatementInvalid:
PGError: ERROR: relation "users" does not exist
问题是,本教程是在 sqlite 中进行的,但我迁移到了 postgres(如前面教程中所建议的那样),所以我也可以了解 postgres。这是造成问题的原因吗?无论如何,activerecord 不应该让我使用什么样的数据库变得透明吗?
我已经创建了正确的数据库,更新了我的 database.yml,运行 db:mgirate 和命令
User.new(name: "Example User", email: "user@example.com")
在沙盒控制台中工作正常。
非常感谢任何帮助。我试图在这里保持领先,对我来说,这是学习 Rails 最具挑战性的部分。几乎感觉就像一个人花费了 90% 的精力来编写和调试测试。(到目前为止,我遇到的唯一绊脚石/头脑弯曲的 headca 是 rspec 测试)
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
--
作为第二个问题,我发现我的 rspec 测试运行了多次?我安装了 ruby-gntp gem,发现每次运行测试时都会出现多次咆哮,并且信息相同。