ruby on rails - Trying to mock an API class with RSpec -
i'm trying learn how use rspec test create_price_type_cache method on setting class being called api class instance. i've tried number of approaches i've googled, figure out how this, or if should write api class differently.
the test below gives me error:
undefined method `api_endpoint' #<rails::application::configuration:0x00000107376420> # setting_spec.rb "should call get_price_type_list api" 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 cachepricetype.delete_all array.each |e| e.save! end end end end # api.rb class api def self.instance @instance ||= new end def get_price_type_list # makes api call , returns array of objects end end
thanks.
edit:
thank jstim waking me up, confused myself after looking @ long. i've stubbed out config settings api class , seems working, best way it, or should mock api class tried?
this current passing test:
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
i'm still not sure if best approach, since i'll have stub every config setting in api class, ended using final test now:
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
Comments
Post a Comment