【问题标题】:What is wrong with this Rspec stub?这个 Rspec 存根有什么问题?
【发布时间】:2016-12-15 18:15:58
【问题描述】:

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

例如

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

【问题讨论】:

  • 你应该使用page[:perms] 而不是page['perms']
  • @Зелёный 有没有更好的方法来解决这个问题?更改我的代码以使其通过规范似乎是个坏主意。尤其是因为实际代码有效。
  • @Зелёный 'page[:perms]' 似乎不是解决方案。还是空的

标签: ruby-on-rails ruby rspec stub


【解决方案1】:

发现问题是我应该改用字典:

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

【讨论】:

    猜你喜欢
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    相关资源
    最近更新 更多