1

我有一个模块

module Foo
  def normalize name
    # modify and return
  end
end

我可以把它混入一个模型就好了......

class Something
  include Foo
end

Something.new.normalize "a string" # works

并尝试混合到控制器...

class SomeController < ApplicationController
  include Foo

  def some_action
    normalize "a string"
  end
end

SomeController#some_action # 在功能测试中工作,但不在 Rails 服务器中!

我尝试了各种形式的模块,扩展 ActiveSupport::Concern,添加包含的块并将规范化更改为类方法,但我得到了相同的结果。为什么这会在功能测试中起作用,而不是在它之外呢?

我觉得我只是错过了一些简单的东西。

4

1 回答 1

3

它在测试中“起作用”的原因是该测试还包括该模块并调用了 normalize 方法:

class SomeControllerTest < ActionController::TestCase
  include Foo

这使它对控制器可用......不知何故。

删除 include Foo 也会导致测试失败。

为了使控制器工作,我改变了

normalize "a string"

self.normalize "a string"
于 2014-09-18T02:58:18.450 回答