我有一个名为 test_feature_spec.rb
的简单测试功能:
(这只是为了解决这个问题……实际规范要长得多,实际功能规范)
require "features_helper"
RSpec.feature "Simple Feature", type: :feature do
scenario "a test scenario" do
get "/"
end
end
我可以做各种 Rails-y 的事情,包括 capybara 驱动的事情(比如
visit such_n_such_path
)。但是上面的崩溃:Failures:
1) Simple Feature a test scenario
Failure/Error: get "/"
NoMethodError:
undefined method `get' for #<RSpec::ExampleGroups::SimpleFeature:0x000000011a799788>
Did you mean? gets
gem
如果我只是将其从
type: :feature
更改为至 type: :request
它工作得很好:> rspec spec/features/test_feature_spec.rb
.
Finished in 0.64913 seconds (files took 5.82 seconds to load)
1 example, 0 failures
似乎与
type
=> request
Rails 或 Rspec 加载了一些东西以允许调用 Controller 路由,但它没有 type
=> feature
.有没有办法加载这些辅助方法(如
get
和 post
等),但仍将功能保留为 type: :feature
? 我知道我可以把它改成
type: :request
但从技术上讲,这些是功能规范,即使对于其中一些,要设置某些状态或执行任何操作,他们也需要调用某些 URL。
最佳答案
看起来有一个解决方案可以解决这个问题:
https://stackoverflow.com/a/24353475/1218280
我问这个的原因有点奇怪。我目前正在将一些 Cucumber 测试重构为 Rspec 功能规范。他们中的一些人正在制作 get
或 post
命令到我们应用程序中的端点作为“设置”步骤。例如,有一个端点用于启用 DB 记录中的某个标志。我可以只设置带有标志集的 DB 对象,但我想尽可能地保持原始 Cucumber 代码的真实性(至少在一开始是这样)。
所以现在我可以这样做(并且它有效):
require "features_helper"
RSpec.feature "Simple Feature", type: :feature do
scenario "a test scenario" do
session = ActionDispatch::Integration::Session.new(Rails.application)
response = session.get("/")
# or I could do: session.post("/api/xyz", params: { one: "one", two: "two" })
end
end
关于ruby-on-rails - 如何向 Rails 中的特性规范添加请求方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50223384/