Rails 控制器规范中的模拟外部类

发布于 2024-12-25 09:37:32 字数 288 浏览 4 评论 0原文

我有一个 Rails 3 应用程序,正在使用 RSpec 进行测试。我有一个使用外部类 MustMock 的控制器,如何

class FooController < ApplicationController
  def myaction
    mockme = MustMock.new
    @foobar = mockme.do_something
  end
end

在我的控制器规范中最好地模拟 MustMock 的实例?

I have a Rails 3 app that I am testing with RSpec. I have a controller using external class MustMock as

class FooController < ApplicationController
  def myaction
    mockme = MustMock.new
    @foobar = mockme.do_something
  end
end

How can I best mock the instance of MustMock in my controller spec?

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

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

发布评论

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

评论(2

左岸枫 2025-01-01 09:37:32
describe FooController do
  specify :myaction do
    MustMock.should_receive(:new)
            .and_return(stub :do_something => :something)
    get :myaction
    assigns[:foobar].should == :something
  end
end
describe FooController do
  specify :myaction do
    MustMock.should_receive(:new)
            .and_return(stub :do_something => :something)
    get :myaction
    assigns[:foobar].should == :something
  end
end
不寐倦长更 2025-01-01 09:37:32

你可以试试这个:

it "does something in myaction" do
    my_stub = stub()
    my_stub.should_receive(:do_something).once.and_return(:foobar)
    MustMock.stub(:new => my_stub)
    get :myaction
    response.should be_success
end

You could try this:

it "does something in myaction" do
    my_stub = stub()
    my_stub.should_receive(:do_something).once.and_return(:foobar)
    MustMock.stub(:new => my_stub)
    get :myaction
    response.should be_success
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文