这个Rspec存根有什么问题?

时间:2016-12-15 18:15:58

标签: ruby-on-rails ruby rspec stub

我已经删除了我的API客户端。并得到它返回预期的哈希。但我无法访问实际的哈希值。

E.g

puts page # returns {:access_token=>"blah", :name=>"parrotcafe", :fb_id=>"2131231", :perms=>"ADMINISTER"}

puts page['perms'] # returns nil instead of 'ADMINISTER'

这是我的Rspec:

  describe 'all' do
    it 'if doesnt exist, it should create authentication' do
      request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:facebook]

      client = double("koala client")
      Koala::Facebook::API.stub(:new).and_return(client)
      allow(client).to receive_messages(get_connections: [{'access_token': 'blah', 'name': 'parrot', 'fb_id': '2131231', 'perms': 'ADMINISTER'}])
      expect {
        post :all, provider: :facebook
      }.to change{ Authentication.count }.by(1)
    end
  end

以下是失败的实际代码。:

  @user_graph = Koala::Facebook::API.new(authentication.token)
  pages = @user_graph.get_connections('me', 'accounts')
  pages.each do |page|
    Page.create(user_id: self.id, access_token: page['access_token'], name: page['name'], fb_id: page['id']) if page['perms'].include? "ADMINISTER"
  end

1 个答案:

答案 0 :(得分:0)

发现问题是我应该使用字典代替:

allow(client).to receive_messages(get_connections: [{'access_token' => 'blah', 'name' => 'parrotcafe', 'fb_id' => '2131231', 'perms' => 'ADMINISTER'}])