English 中文(简体)
Mocking/stubbing a methods that s included from “instance.extend (Decorator Module)”
原标题:Mocking/stubbing a method that s included from "instance.extend(DecoratorModule)"

I use a decorator module that get s included in a model instance (through the "extends" method). So for example :

module Decorator
  def foo
  end
end

class Model < ActiveRecord::Base
end

class ModelsController < ApplicationController
  def bar
    @model = Model.find(params[:id])
    @model.extend(Decorator)
    @model.foo
  end
end

Then I would like in the tests to do the following (using Mocha) :

test "bar" do
  Model.any_instance.expects(:foo).returns("bar")
  get :bar
end 

难道这种可能性是多少,或者你是否想到其他方式来取得这一功能?

最佳回答

它发挥作用(在测试申请中确认,原文如此)。

我通常包括矫正器(而不是在运行时予以延伸),我避免任何伤害,因为这种做法被认为是不良的做法(我发现相反)。

module Decorators
  module Test
    def foo
      "foo"
    end
  end
end

class MoufesController < ApplicationController

  def bar
    @moufa = Moufa.first
    @moufa.extend(Decorators::Test)
    render :text => @moufa.foo
  end
end

require  test_helper 

class MoufesControllerTest < ActionController::TestCase
  # Replace this with your real tests.
  test "bar" do
    m = Moufa.first
    Moufa.expects(:find).returns(m)
    m.expects(:foo).returns("foobar")

    get :bar, {:id => 32}
    assert_equal @response.body, "foobar"
  end
end
问题回答

Just an Assumption Note: I will assume that your Decorator foo method returns "bar" which is not shown in the code that you sent. If I do not assume this, then expectations will fail anyway because the method returns nil and not "bar".

综上所述,我尝试了整个故事,因为你有光彩的新铁路,我已经认识到,不能这样做。 这是因为,在expects时,该方法的疏漏并非 随附<>>>> /em>。 你们的检验要求采用方法。

我到这里来的是这一结论。 在那里,*hide_> ́al_method*没有发现任何藏匿和藏匿的方法。 接着<>>。 这种方法并不包括用于执行您的“期望”方法,而是实际的“<>->->--->-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

我的回答是,不可能这样做。

Ok, now I understand. You want to stub out a call to an external service. Interesting that mocha doesn t work with extend this way. Besides what is mentioned above, it seems to be because the stubbed methods are defined on the singleton class, not the module, so don t get mixed in.

为什么没有这样的东西?

test "bar" do
  Decorator = Module.new{ def foo;  foo ; end }
  get :bar
end

如果你没有获得关于已定义的“决定者”的警告——这暗示有点争 coup,你可以注射:

class ModelsController < ApplicationController
  class << self
    attr_writer :decorator_class
    def decorator_class; @decorator_class ||= Decorator; end
  end

  def bar
    @model = Model.find(params[:id])
    @model.extend(self.class.decorator_class)
    @model.foo
  end
end

检验标准如下:

test "bar" do
  dummy = Module.new{ def foo;  foo ; end }
  ModelsController.decorator_class = dummy
  get :bar
end

当然,如果你的情况较为复杂,有多个矫正员或鉴定人界定多种方法,这或许不会对你有用。

但我认为,这好于对发现的推测。 一般来说,你不想在一体化测试中困扰你的模式。

如果你想测试以下物品的回报价值,则稍有改动:

test "bar" do
  Model.any_instance.expects(:foo).returns("bar")
  assert_equal "bar", get(:bar)
end

但是,如果你只是检验一个示范案例是否有校正方法,那么你真的需要加以检验吗? 看来像你一样,在该案中测试目标编号。

如果你想测试@model的行为。 foo, 你不需要在一体化测试中这样做——这有利于cor子的优点,那么你可以像孤立一样测试它。

x = Object.new.extend(Decorator)
#.... assert something about x.foo ...

在我的经验中,参加融合测试通常是一种密码。





相关问题
Selenium not working with Firefox 3.x on linux

I am using selenium-server , selenium rc for UI testing in my application . My dev box is Windows with FireFox 3.5 and every thing is running fine and cool. But when i try to run selenium tests on my ...

Best browser for testing under Safari Mobile on Linux?

I have an iPhone web app I m producing on a Linux machine. What s the best browser I can use to most closely mimic the feature-limited version of Safari present on the iPhone? (It s a "slimmed down" ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Is there any error checking web app cralwers out there?

Wondering if there was some sort of crawler we could use to test and re-test everything when changes are made to the web app so we know some new change didn t error out any existing pages. Or maybe a ...

热门标签