如何让instance_of与RoR中的ActiveRecord对象一起使用?

发布于 2024-08-24 05:42:57 字数 707 浏览 6 评论 0原文

今天,我在使用 RoR 来存根对 AR 对象的调用时遇到了一个问题。我认为我能够做一些类似的事情:

stub.instance_of(BankAccount).save_to_other_spot { true }

但是,当我尝试这种方法时,它似乎根本没有存根该方法,并且最终会运行我试图存根的原始方法。我使用调试器等确认了这一点。

所以我最终使用了以下方法:

stub.proxy(BankAccount).find(anything) do |account|
  stub(account).save_to_other_spot { true }
  account
end

这有效。

我想知道我是否做错了什么?为什么 instance_of 没有按照我期望的方式工作?

我遇到的另一个问题是,在 RSpec 测试中,我似乎必须为每个请求设置模拟和存根。再说一遍,这是正常现象还是我做错了什么?

我的意思是我必须做类似的事情:

... mock and stub ...
get :show, :id => @id

... mock and stub ...
post :update, :id => id, :account => { ... params ... }

我认为我能够在顶部进行模拟和存根。

Today I ran into an issue using RoR to stub calls to AR objects. I thought that I'd be able to do something along the lines of :

stub.instance_of(BankAccount).save_to_other_spot { true }

However when I tried this method it didn't seem to stub the method at all and it would end up running the original method I was trying to stub. I confirmed this using debugger etc.

So I ended up using the following method :

stub.proxy(BankAccount).find(anything) do |account|
  stub(account).save_to_other_spot { true }
  account
end

This works.

I was wondering if I'm doing something wrong though? Why doesn't instance_of work in the way I expect?

Another issue I ran into was that in my RSpec tests I seem to have to setup my mocks and stubs for each request. Again, is this normal or am I doing something wrong?

By this I mean I'd have to do something like :

... mock and stub ...
get :show, :id => @id

... mock and stub ...
post :update, :id => id, :account => { ... params ... }

I thought I'd be able to mock and stub once at the top.

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

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

发布评论

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

评论(1

向地狱狂奔 2024-08-31 05:42:57

假设您正在运行 RSpec >2.5...,则模拟/存根语法已得到改进,以便您现在可以使用以下定义。

BankAccount.any_instance.stub(:save_to_other_spot) { true }

请注意,您将需要使用RSpec 的最新版本。 RSpec 的早期版本不包含 any_instance 方法。看起来他们从 Mocha 借用了这个并将其实现到 RSpec 模拟中。

如果您使用的是旧版本的 RSpec,那么我认为您正在做的事情是唯一的方法。只是我倾向于这样写:

@bank_account = BankAccount.new
BankAccount.stub(:find) { @bank_account }
@bank_account.stub(:save_to_other_spot) { true }

尽管我认为你的块方法看起来更干净。

Assuming you are running RSpec >2.5... then the mock/stubbing syntax has been improved so that you can now use the following definition.

BankAccount.any_instance.stub(:save_to_other_spot) { true }

Note you will need to use a latter version of RSpec. The earlier versions of RSpec do not include the any_instance method. It looks like they borrowed this from Mocha and implemented it into RSpec mocks.

If you are using the older versions of RSpec, then what you're doing I think is the only way. Only that I tend to write it like this:

@bank_account = BankAccount.new
BankAccount.stub(:find) { @bank_account }
@bank_account.stub(:save_to_other_spot) { true }

Albeit I think your block method looks cleaner.

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