用ruby模拟第三方对象的最佳方法是什么?

我正在使用twitter gem编写测试应用程序,我想编写一个集成测试,但我无法弄清楚如何模拟Twitter命名空间中的对象。 这是我想测试的功能:

def build_twitter(omniauth)
  Twitter.configure do |config|
    config.consumer_key = TWITTER_KEY
    config.consumer_secret = TWITTER_SECRET
    config.oauth_token = omniauth['credentials']['token']
    config.oauth_token_secret = omniauth['credentials']['secret']
  end
  client = Twitter::Client.new
  user = client.current_user
  self.name = user.name
end

这里是我试图编写的rspec测试:

feature 'testing oauth' do
  before(:each) do
    @twitter = double("Twitter")
    @twitter.stub!(:configure).and_return true
    @client = double("Twitter::Client")
    @client.stub!(:current_user).and_return(@user)
    @user = double("Twitter::User")
    @user.stub!(:name).and_return("Tester")
  end

  scenario 'twitter' do

    visit root_path
    login_with_oauth

    page.should have_content("Pages#home")
  end
end

但是,我收到这个错误:

1) testing oauth twitter
   Failure/Error: login_with_oauth
   Twitter::Error::Unauthorized:
     GET https://api.twitter.com/1/account/verify_credentials.json: 401: Invalid / expired Token
   # ./app/models/user.rb:40:in `build_twitter'
   # ./app/models/user.rb:16:in `build_authentication'
   # ./app/controllers/authentications_controller.rb:47:in `create'
   # ./spec/support/integration_spec_helper.rb:3:in `login_with_oauth'
   # ./spec/integration/twit_test.rb:16:in `block (2 levels) in <top (required)>'

上面的模拟使用rspec,但我也可以尝试摩卡。 任何帮助将不胜感激。

好的,我得到了大家的帮助,才弄清楚了这一点。 这是最后的测试:

feature 'testing oauth' do
  before(:each) do
    @client = double("Twitter::Client")
    @user = double("Twitter::User")
    Twitter.stub!(:configure).and_return true
    Twitter::Client.stub!(:new).and_return(@client)
    @client.stub!(:current_user).and_return(@user)
    @user.stub!(:name).and_return("Tester")
  end

  scenario 'twitter' do

    visit root_path
    login_with_oauth

    page.should have_content("Pages#home")
  end
end

诀窍是弄清楚我需要在实例对象实例上存储:configure:new ,并在存储对象实例上创建存根:current_user:name


我认为问题就在于你使用模拟的方式,你创建了模拟@twitter,但你从来没有真正使用它。 我认为你可能会觉得任何对Twitter的调用都会使用你指定的存根方法,但这不是它的工作方式,只有对@twitter的调用被存根。

我使用双ruby,而不是rspec mocks,但我相信你想要做这样的事情:

Twitter.stub!(:configure).and_return true
...
Twitter::Client.stub!(:current_user).and_return @user

这可以确保您随时在Twitter上Twitter,Twitter :: Client的方法被调用,他们响应您的想法。

此外,这似乎很奇怪,这是作为视图的一部分进行测试,应该真正成为控制器测试的一部分,而不是我错过了一些东西。


您可以尝试使用http://jondot.github.com/moxy/。 模拟Web请求

链接地址: http://www.djcxy.com/p/56461.html

上一篇: What is the best way to mock a 3rd party object in ruby?

下一篇: register PSOCK cluster with foreach