RSpec Stubbing:返回参数

发布于 2024-11-06 06:58:31 字数 201 浏览 1 评论 0原文

虽然我的问题非常简单,但我未能在这里找到答案:

如何存根方法并返回参数本身(例如在执行数组操作的方法上)?

像这样的东西:

 interface.stub!(:get_trace).with(<whatever_here>).and_return(<whatever_here>)

Though my question is pretty straightforward, I failed to find an answer around here:

How can I stub a method and return the parameter itself (for example on a method that does an array-operation)?

Something like this:

 interface.stub!(:get_trace).with(<whatever_here>).and_return(<whatever_here>)

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

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

发布评论

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

评论(3

嗼ふ静 2024-11-13 06:58:31

注意:stub 方法已被弃用。请参阅此答案了解执行此操作的现代方法。


stub! 可以接受一个块。该块接收参数;块的返回值是存根的返回值:

class Interface
end

describe Interface do
  it "should have a stub that returns its argument" do
    interface = Interface.new
    interface.stub!(:get_trace) do |arg|
      arg
    end
    interface.get_trace(123).should eql 123
  end
end

Note: The stub method has been deprecated. Please see this answer for the modern way to do this.


stub! can accept a block. The block receives the parameters; the return value of the block is the return value of the stub:

class Interface
end

describe Interface do
  it "should have a stub that returns its argument" do
    interface = Interface.new
    interface.stub!(:get_trace) do |arg|
      arg
    end
    interface.get_trace(123).should eql 123
  end
end
醉酒的小男人 2024-11-13 06:58:31

存根方法已被弃用,取而代之的是expect。

expect(object).to receive(:get_trace).with(anything) do |value| 
  value
end

https://relishapp.com/rspec/rspec-mocks/ v/3-2/docs/configuring-responses/block-implementation

The stub method has been deprecated in favor of expect.

expect(object).to receive(:get_trace).with(anything) do |value| 
  value
end

https://relishapp.com/rspec/rspec-mocks/v/3-2/docs/configuring-responses/block-implementation

情深缘浅 2024-11-13 06:58:31

您可以使用 allow (存根)而不是 expect (模拟):

allow(object).to receive(:my_method_name) { |param1, param2| param1 }

使用命名参数:

allow(object).to receive(:my_method_name) { |params| params[:my_named_param] }

这是一个现实生活中的示例:

假设我们有一个 S3StorageService 使用 upload_file 方法将我们的文件上传到 S3。该方法返回我们上传文件的 S3 直接 URL。

def self.upload_file(file_type:, pathname:, metadata: {}) …

我们想要存根上传的原因有很多(离线测试、性能改进……):

allow(S3StorageService).to receive(:upload_file) { |params| params[:pathname] }

该存根仅返回文件路径。

You can use allow (stub) instead of expect (mock):

allow(object).to receive(:my_method_name) { |param1, param2| param1 }

With named parameters:

allow(object).to receive(:my_method_name) { |params| params[:my_named_param] }

Here is a real life example:

Let's assume we have a S3StorageService that uploads our files to S3 using the upload_file method. That method returns the S3 direct URL to our uploaded file.

def self.upload_file(file_type:, pathname:, metadata: {}) …

We want to stub that upload for many reasons (offline testing, performance improvements…):

allow(S3StorageService).to receive(:upload_file) { |params| params[:pathname] }

That stub only returns the file path.

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