gpt4 book ai didi

ruby-on-rails - Rspec 生成器创建 *_spec.rb 文件来测试 PORO

转载 作者:太空宇宙 更新时间:2023-11-03 18:11:57 26 4
gpt4 key购买 nike

This website列出可用于为特定 Rails 文件创建规范文件的各种生成器

我知道如何为模型生成规范文件。

例如:rails generate rspec:model Customer 创建这个文件:

#spec/models/customer_spec.rb
require 'rails_helper'

RSpec.describe Customer, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

我创建了一个服务:MYPORO。它是一个 PORO(普通旧 ruby​​ 对象),位于 app/services/my_poro.rb

如何生成 spec 文件以便为 MyPoro 类及其实例创建测试?我遇到的最大问题是我不知道要提供什么 type,因为它不是 :model。我只是手动创建文件,但又一次:我不知道要在文件中提供什么 :type

最佳答案

PORO 不需要“类型”

The biggest issue I have is I don't know what type to provide, because it is not a :model. I would just manually create the file, but again: I don't know what :type to provide inside the file.

您不需要为普通 Ruby 对象指定类型。 :type 告诉由 rails_helper 模块(由 rspec-rails 安装)加载的 RSpec Rails 扩展如何对模型、路由等运行某些类型的测试。

对于这些类型的规范,我通常仍然加载 rails_helper 而不是 spec_helper,但在大多数情况下,您应该能够简化为如下内容:

require 'spec_helper'

describe Customer do
pending "insert tests on your non-Rails Customer class"
end

关于ruby-on-rails - Rspec 生成器创建 *_spec.rb 文件来测试 PORO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32404079/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com