Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 试图用RSpec模拟API类_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

Ruby on rails 试图用RSpec模拟API类

Ruby on rails 试图用RSpec模拟API类,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我试图学习如何使用RSpec来测试设置类上的create_price_type_cache方法是否被API类实例调用。我尝试过谷歌搜索过的许多方法,但我知道如何做到这一点,甚至我是否应该以不同的方式编写API类 下面的测试给出了错误: undefined method `api_endpoint' for #<Rails::Application::Configuration:0x00000107376420> # setting_spec.rb it "should call

我试图学习如何使用RSpec来测试设置类上的create_price_type_cache方法是否被API类实例调用。我尝试过谷歌搜索过的许多方法,但我知道如何做到这一点,甚至我是否应该以不同的方式编写API类

下面的测试给出了错误:

undefined method `api_endpoint' for #<Rails::Application::Configuration:0x00000107376420>


# setting_spec.rb
it "should call get_price_type_list from the API" do
  price_types = [build(:price_type)]

  api = mock_model(Api.instance)
  api.stub!(:get_price_type_list).and_return(price_types)
  expect(Api.instance.should_recieve(:get_price_type_list).and_return(api)).to eq [price_types]
end

# setting.rb
class Setting
  def self.create_price_type_cache
    array = Api.instance.get_price_type_list
    ActiveRecord::Base.transaction do
      CachePriceType.delete_all
      array.each do |e|
        e.save!
      end
    end
  end
end

# api.rb
class Api
  def self.instance
    @instance ||= new
  end

  def get_price_type_list
    # Makes an API call and returns an array of objects
  end
end

我仍然不确定这是否是最好的方法,因为我必须在我的Api类中存根每个配置设置,但这是我现在最后使用的测试:

Rails.application.config.stub(:api_endpoint).and_return("")
price_types = [build(:cache_price_type)]
Api.instance.stub(:get_price_type_list).and_return([price_types])
Array.any_instance.stub(:save!)
expect {Setting.create_price_type_cache}.to_not raise_error

我不明白你为什么在期待中称之为嘲弄。这就是您应该调用
设置的地方。创建\u price\u type\u cache
。我不知道这是否能通过测试,但这是我突然想到的第一个问题。
Rails.application.config.stub(:api_endpoint).and_return("")
price_types = [build(:cache_price_type)]
Api.instance.stub(:get_price_type_list).and_return([price_types])
Array.any_instance.stub(:save!)
expect {Setting.create_price_type_cache}.to_not raise_error