代码之家  ›  专栏  ›  技术社区  ›  Steve Weet

有没有一种方法可以撤销任何一个实例的Mocha存根?

  •  4
  • Steve Weet  · 技术社区  · 16 年前

    在我的控制器规格,我是存根有效吗?对于一些路由测试,(基于Ryan Bates niftyïu scaffold),如下所示:-

    it "create action should render new template when model is invalid" do
      Company.any_instance.stubs(:valid?).returns(false)
      post :create
      response.should render_template(:new)
    end
    

    it "is valid with valid attributes" do
      @company.should be_valid
    end
    

    同样地,当单独测试时,它工作良好。如果我同时为模型和控制器运行spec,问题就来了。模型测试总是失败作为有效的?方法已被取消。当控制器测试被拆掉时,有没有一种方法可以让我删除任何\u实例的存根。

    我已经绕过了这个问题,以相反的字母顺序运行测试,以确保模型测试在控制器之前运行,但我真的不喜欢我的测试依赖于顺序。

    3 回复  |  直到 5 年前
        1
  •  4
  •   Simone Carletti    15 年前

    您需要手动配置RSpec。

    Rspec.configure do |config|
      ...
    
      # == Mock Framework
      #
      # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
      #
      config.mock_with :mocha
      # config.mock_with :flexmock
      # config.mock_with :rr
      # config.mock_with :rspec
    end
    

    http://rspec.info/documentation/mocks/message_expectations.html

        2
  •  3
  •   fguillen    15 年前

    我有同样的问题,但没有使用 Rspec 但很正常 Test::Unit ,实际上 ActionController::TestCase

    期望

    • 红宝石:1.9.2
    • 轨道:3.0.3

    更新 unstub 摩卡咖啡法: http://mocha.rubyforge.org/classes/Mocha/ObjectMethods.html#M000009

        3
  •  1
  •   anshul    16 年前

    您的spec\u助手是否包含

    Spec::Runner.configure do |config|
      config.mock_with :mocha
    end
    

    推荐文章