Question

I have a loop like this defined:

 api.release(release_url).rels[:assets].get.data.each do |existing_file|
   log "Already Contains: #{existing_file.name}"
 end

How can I stub existing_file.name? I want it give it multiple values as well.

I tried to stub it like this:

      provider.api.release.rels[:assets].get.stub(:data).and_return([{ :name => "foo.bar"}, { :name => "bar.foo"])

But this returns the error: undefined method 'name' for Hash. This makes me think that I am iterating through a array of classes. Any advice on how to stub this? Thanks for your help!

Was it helpful?

Solution

Use double - you should do something like this:

provider.api.release.rels[:assets].get.stub(:data).and_return(
  [double(:name => "foo.bar"), double(:name => "bar.foo")]
)

OTHER TIPS

Try this:

Create a helper class, let us call it Clazz, and use that instead of hashes.

class Clazz < Struct.new(:name); end;

provider.api.release.rels[:assets].get.stub(:data).and_return([Clazz.new("name1"), Clazz.new("name2")])

Now you can use and call name as a method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top