【问题标题】:Using mocha for controller in functional test with RSPEC在 RSPEC 的功能测试中使用 mocha 作为控制器
【发布时间】:2011-03-16 21:42:16
【问题描述】:

我在这里使用 Rspec 做一些测试,我想确保控制器在某些操作中调用 log 方法。我也在用摩卡。

我想要这样的东西:

it "update action should redirect when model is valid" do
    Tag.any_instance.stubs(:valid?).returns(true)
    put :update, :id => Tag.first
    controller.expects(:add_team_log).at_least_once
    response.should redirect_to(edit_admin_tag_url(assigns[:tag]))
  end

有什么东西可以用作“控制器”变量吗?我试过self,控制器类名...

【问题讨论】:

  • 需要查看您的控制器进行编码以了解这里发生了什么。否则它看起来就像它没有达到你的期望。

标签: ruby-on-rails controller rspec functional-testing mocha.js


【解决方案1】:

我刚刚得到了帮助。对于测试控制器,您可以将规范嵌套在为控制器命名的描述中。 (规范也应该在 Controllers 文件夹中)

describe ArticlesController do
  integrate_views
    describe "GET index" do
     ...
      it "update action should redirect when model is valid" do
         ...
        controller.expects(:add_team_log).at_least_once
    ...
     end
   end

结束

【讨论】:

    【解决方案2】:

    我认为你想要@controller 而不是控制器。这是我的测试套件中的一个示例:

    it "delegates to the pricing web service" do
      isbn = "an_isbn"
      @controller.expects(:lookup)
        .with(isbn, anything)
        .returns({asin: "an_asin"})
    
      get :results, isbn: isbn
      assert_response :success
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多