在 RSPEC 功能测试中使用 mocha 作为控制器

发布于 2024-10-22 20:33:20 字数 419 浏览 7 评论 0原文

我在这里使用 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,控制器类名......

I'm doing some tests here using Rspec and I would like to assure that the controller is calling the log method in some actions. I'm also using mocha.

I would like something like this:

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

is there something to use as the 'controller' variable? I tried self, the controller class name...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒江雪… 2024-10-29 20:33:20

我刚刚得到了这方面的帮助。为了测试控制器,您可以将规范嵌套在命名控制器的描述中。 (规范也应该位于 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

结束

I just got helped with this. For testing controllers, you'd nest your specs inside a describe which names the controller. (The spec should also be in the Controllers folder)

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

end

冷血 2024-10-29 20:33:20

我认为你想要 @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

I think you want @controller instead of controller. Here's an example from my test suite:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文