如何模拟已模拟对象的实例方法?

发布于 2024-09-28 23:50:43 字数 556 浏览 4 评论 0原文

我需要模拟以下内容:

Class User
  def facebook
    #returns an instance of a facebook gem
  end
end

因此,在我的用户测试中,要访问用户的 facebook 信息,我需要调用 user.facebook.me.info 来检索其信息。如果我想嘲笑这个,我目前正在使用以下内容:

@user = Factory(:user)
facebook = mock()
me = mock()
me.expects(:info).returns({"name" => "John Doe"})
facebook.expects(:me).returns(me)
@user.expects(:facebook).returns(facebook)
assert_equal "John Doe", @user.facebook.me.info["name"]

这有效,但似乎有点笨拙,有更好的方法吗?

[编辑] 我使用 mocha 作为模拟框架

I need to mock the following:

Class User
  def facebook
    #returns an instance of a facebook gem
  end
end

So in my User tests, to access the User's facebook info I need to call user.facebook.me.info to retrieve its info. If I want to mock this, I'm currently using the following:

@user = Factory(:user)
facebook = mock()
me = mock()
me.expects(:info).returns({"name" => "John Doe"})
facebook.expects(:me).returns(me)
@user.expects(:facebook).returns(facebook)
assert_equal "John Doe", @user.facebook.me.info["name"]

This works but seems a bit unwieldy, is there a better way to do this ?

[edit] I'm using mocha as mocking framework

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

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

发布评论

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

评论(2

迷途知返 2024-10-05 23:50:43

你可以尝试这样的事情:-

user = Factory(:user)
user.stubs(:facebook => stub(:me => stub(:info => {:name => "John Doe"})))

如果你真的想检查所有这些方法是否被调用(我怀疑你没有),你可以执行以下操作:-

user = Factory(:user)
user.expects(:facebook => mock(:me => mock(:info => {:name => "John Doe"})))

它有点冗长,但通常值得给每个模拟反对一个名字:-

user = Factory(:user)
user.stubs(:facebook => stub('facebook', :me => stub('me', :info => {:name => "John Doe"})))

我希望有帮助。

You could try something like this :-

user = Factory(:user)
user.stubs(:facebook => stub(:me => stub(:info => {:name => "John Doe"})))

If you really want to check that all these methods are called (which I suspect you don't), you could do the following :-

user = Factory(:user)
user.expects(:facebook => mock(:me => mock(:info => {:name => "John Doe"})))

It's a bit more verbose, but it's usually worthwhile giving each mock object a name :-

user = Factory(:user)
user.stubs(:facebook => stub('facebook', :me => stub('me', :info => {:name => "John Doe"})))

I hope that helps.

羁拥 2024-10-05 23:50:43

如果您不想检查是否调用了所有方法,您还可以使用不同的模拟替代方法。例如,您可以使用 OpenStruct。

@user = Factory(:user)
facebook = mock()
me = mock()
me.expects(:info).returns({"name" => "John Doe"})
facebook.expects(:me).returns(me)
@user.expects(:facebook).returns(facebook)
assert_equal "John Doe", @user.facebook.me.info["name"]

这个解决方案还为您提供了

@user = Factory(:user)
@facebook = OpenStruct.new(:me => OpenStruct.new(:info => {:name => "John Doe"}))
@user.expects(:facebook).returns(@facebook)

一个优势,如果您需要测试不同的条件,您可以在测试中更改@facebook 属性。

If you don't want to check that all the methods are called, you can also use different alternatives to mocking. For instance, you can use an OpenStruct.

@user = Factory(:user)
facebook = mock()
me = mock()
me.expects(:info).returns({"name" => "John Doe"})
facebook.expects(:me).returns(me)
@user.expects(:facebook).returns(facebook)
assert_equal "John Doe", @user.facebook.me.info["name"]

becomes

@user = Factory(:user)
@facebook = OpenStruct.new(:me => OpenStruct.new(:info => {:name => "John Doe"}))
@user.expects(:facebook).returns(@facebook)

This solution also offers you the advantage that you can change the @facebook properties in your tests, if you need to test different conditions.

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