运行 rspec 为了 rails 需要什么先决条件?
What are the pre-requisties that I need to run rspec for rails?
我的团队 rails 代码有 ruby,现在我需要使用 rspec 对规范进行单元测试。我已经浏览了几个网站,但没有得到如何使用它的完美镜头。谁能帮我完成使用 rspec 的步骤。以及如何在测试数据库中创建用户。请指导我,我是 rspec.
的新手
关于设置 RSpec 的一个很好的指南是从 rspec repo page on GitHub. 上的自述文件中获得的。
但是...就个人而言,以下是我最常使用的组合:
- Database cleaner: For cleaning the db as the name implies. A good guide on setting this up is as given by Avdi on his blog here.
注意:本博客建议将数据库清理器的配置放在支持目录中的单独文件中。如果这样做了,请记住在 rails_helper.rb
文件
中取消注释以下行
# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
这将确保测试套件中需要支持目录下的所有文件。
- FactoryGirls: This is for generating test records(users and etc) just like you ask in the question. A good enough guide is as given on the getting started document here.
- Faker:我用它来生成
Fake
随机值来构建我的记录。例如:测试用户的 first_name、last_name、电子邮件、密码等,而不是对这些值进行硬编码。使用它就像 repo 中的自述文件中所述的那样直接
正确配置以上内容后,第一个 Rspec 规格就可以编写了。
规格基本上可以分为两步:
- 描述
- 预期
描述是对套装正在测试的内容的总结,而期望是正常的预期结果。
描述也可以嵌套在其他描述中。
Rspec 测试的一个例子是:
require 'rails_helper'
describe TextMessage do
describe "#failed?" do
it "returns true" do
text = FactoryGirl.create(:text_message, :not_delivered)
expect(text.send(:failed?)).to be_truthy
end
end
end
首先,需要 rails_helper
文件。这是所有测试配置所在的位置。当 rails generate rspec:install
命令是来自 Rspec 安装指南的 运行 时自动生成此文件。
接下来,我有一个描述块 describe TextMessage do;end
这是描述我的模型,在这种情况下是 TextMessage
接下来,我将另一个描述块嵌套在第一个描述我的 TextMessage 模型中的方法(失败?)中:describe "#failed?" do;end
然后我有一个期望块,这是我的主要测试:
it "returns true" do
text = FactoryGirl.create(:text_message, :not_delivered)
expect(text.send(:failed?)).to be_truthy
end
failed?
方法的所有其他测试都将在此之下,及其各自的块...而模型中其他方法的其他测试将相应地在第一个之下。
希望我能在这里为您解决这个问题。
我的团队 rails 代码有 ruby,现在我需要使用 rspec 对规范进行单元测试。我已经浏览了几个网站,但没有得到如何使用它的完美镜头。谁能帮我完成使用 rspec 的步骤。以及如何在测试数据库中创建用户。请指导我,我是 rspec.
的新手关于设置 RSpec 的一个很好的指南是从 rspec repo page on GitHub. 上的自述文件中获得的。
但是...就个人而言,以下是我最常使用的组合:
- Database cleaner: For cleaning the db as the name implies. A good guide on setting this up is as given by Avdi on his blog here.
注意:本博客建议将数据库清理器的配置放在支持目录中的单独文件中。如果这样做了,请记住在 rails_helper.rb
文件
# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
这将确保测试套件中需要支持目录下的所有文件。
- FactoryGirls: This is for generating test records(users and etc) just like you ask in the question. A good enough guide is as given on the getting started document here.
- Faker:我用它来生成
Fake
随机值来构建我的记录。例如:测试用户的 first_name、last_name、电子邮件、密码等,而不是对这些值进行硬编码。使用它就像 repo 中的自述文件中所述的那样直接
正确配置以上内容后,第一个 Rspec 规格就可以编写了。
规格基本上可以分为两步:
- 描述
- 预期
描述是对套装正在测试的内容的总结,而期望是正常的预期结果。
描述也可以嵌套在其他描述中。
Rspec 测试的一个例子是:
require 'rails_helper'
describe TextMessage do
describe "#failed?" do
it "returns true" do
text = FactoryGirl.create(:text_message, :not_delivered)
expect(text.send(:failed?)).to be_truthy
end
end
end
首先,需要 rails_helper
文件。这是所有测试配置所在的位置。当 rails generate rspec:install
命令是来自 Rspec 安装指南的 运行 时自动生成此文件。
接下来,我有一个描述块 describe TextMessage do;end
这是描述我的模型,在这种情况下是 TextMessage
接下来,我将另一个描述块嵌套在第一个描述我的 TextMessage 模型中的方法(失败?)中:describe "#failed?" do;end
然后我有一个期望块,这是我的主要测试:
it "returns true" do
text = FactoryGirl.create(:text_message, :not_delivered)
expect(text.send(:failed?)).to be_truthy
end
failed?
方法的所有其他测试都将在此之下,及其各自的块...而模型中其他方法的其他测试将相应地在第一个之下。
希望我能在这里为您解决这个问题。