如何在RSPEC中存根ShopifyAPI :: Theme.all请求

时间:2018-04-12 07:35:15

标签: ruby-on-rails rspec-rails shopify-app

我正在进行RSPEC测试,需要存根或发出虚假请求来返回静态哈希。

我有

ShopifyAPI::Theme.all.select{|t| t.role == "main"}.first

我知道存根ShopifyAPI::Theme.all

allow(ShopifyAPI::Theme).to receive(:all).and_return( test_main_theme )

我有辅助方法。

 def test_main_theme
  {
   "id": 2335539244,
   "name": "Debut",
   "created_at": "2017-12-22T18:13:24-05:00",
   "updated_at": "2018-04-11T20:16:17-04:00",
   "role": "main",
   "theme_store_id": 796,
   "previewable": true,
   "processing": false
 }
end

.select{|t| t.role == "main"}.first是另一种方式。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以尝试:

allow(ShopifyAPI::Theme).to receive_message_chain(:all, :select, :first)
  .and_return(test_main_theme)

请参阅https://relishapp.com/rspec/rspec-mocks/v/3-5/docs/working-with-legacy-code/message-chains