Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jtzemp
Created August 26, 2009 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jtzemp/175710 to your computer and use it in GitHub Desktop.
Save jtzemp/175710 to your computer and use it in GitHub Desktop.
# code
class ExternalDataApi
def initialize(setup)
# do stuff to setup
end
def list_of_things
# grabs stuff from the api and returns an array of OStructs
end
end
class Thing << ActiveRecord::Base
def self.load_stuff_from_api
api = ExternalDataApi.new(setup)
api.list_of_things.each do |thing|
Stuff.create(
:name => thing.name,
:etc => thing.etc
)
end
end
end
# spec
describe Thing, ".load_stuff_from_api" do
it "populates things from the api" do
fake_list = [OpenStruct.new(:name => "name", :etc => "etc")]
### Not sure what to do right here
api = mock(ExternalDataAPI)
api.should_receive(:list_of_things).once.and_return(fake_list)
### / Not sure what to do right here
Thing.all.should == []
Thing.load_stuff_from_api
Thing.all.map(&:name).should == fake_list.map(&:name)
Thing.all.map(&:etc).should == fake_list.map(&:etc)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment