ruby-on-rails - 是否可以使用 ruby​​ 改进来更改测试中 Controller 操作的行为?

标签 ruby-on-rails ruby stubbing refinements

是否可以使用优化功能来 stub Controller 操作?

我在“my_controller_refinement.rb”中定义细化

require "my_controller"

module MyControllerRefinement
  refine MyController do
    def create_item(my_object = {})
      return "test_id"
    end
  end
end

并在测试中使用它,如下 -

require_relative "my_controller_refinement"

class MyControllerTest < ActionController::TestCase
  using MyControllerRefinement

  test "get item" do
    post :create { my_object: { name: "Test", id: "test_id" } }
    # Post redirects to the show page
    assert_redirected_to action: "show", id: "test_id"
  end
end

测试目录为 -

test/
  -->  my_controller_refinement.rb
  -->  my_controller_test.rb

但是细化并没有开始,实际的 Controller 操作似乎被调用了。

我是否遗漏了一些东西,或者改进不能用于这种“ stub ”?

最佳答案

这行不通,因为 Refinements目前工作。文档(如下引用)提供了完整的独家新闻,但实质上细化的范围非常狭窄。

You may only activate refinements at top-level, not inside any class, module or method scope. You may activate refinements in a string passed to Kernel#eval that is evaluated at top-level. Refinements are active until the end of the file or the end of the eval string, respectively.

Refinements are lexical in scope. When control is transferred outside the scope the refinement is deactivated. This means that if you require or load a file or call a method that is defined outside the current scope the refinement will be deactivated.

关于ruby-on-rails - 是否可以使用 ruby​​ 改进来更改测试中 Controller 操作的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44052660/

相关文章:

mysql - Rails+MySQL 在生产环境中:数据库配置未指定适配器 (ActiveRecord::AdapterNotSpecified)

java - PowerMockito。模拟静态方法。为什么 PowerMockito#stub 不起作用?

mysql - 安装 rails

sql - Rails 查找等于两个日期之间的日期

ruby-on-rails - 质量增量场加 1

ruby - 删除散列中不在数组中的键

Ruby Net::SFTP 传输中断处理

ruby - 确保值在集合中,如果不在集合中则设置默认值

java - Mockito 使用匹配器进行 stub

ruby-on-rails - 模型中未定义的任何实例