I want to use ActiveResource in my app. Instead of hitting server though, I want it to load from a file when I call find. Any clue how I do that? Pat
On 2/7/07, Pat Maddox <pergesu at gmail.com> wrote:> I want to use ActiveResource in my app. Instead of hitting server > though, I want it to load from a file when I call find. Any clue how > I do that? >In the same way as you''d mock ActiveRecord I would think. Is this not working for you? Aslak> Pat > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
I think you need to stub the ActiveResource methods like ''create'' and ''find'', assuming the ActiveResource library is well tested you should be able to test only the behaviour you are writing instead of the REST communication. On 2/7/07, Pat Maddox <pergesu at gmail.com> wrote:> I want to use ActiveResource in my app. Instead of hitting server > though, I want it to load from a file when I call find. Any clue how > I do that? > > Pat > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On 2/7/07, aslak hellesoy <aslak.hellesoy at gmail.com> wrote:> On 2/7/07, Pat Maddox <pergesu at gmail.com> wrote: > > I want to use ActiveResource in my app. Instead of hitting server > > though, I want it to load from a file when I call find. Any clue how > > I do that? > > > > In the same way as you''d mock ActiveRecord I would think. Is this not > working for you?No I want it to actually go through the process of finding something, but just have it use a file rather than an actual server. Takes a couple steps (with my current solution): 1. Mock Net::HTTP.new to return some mock http object 2. mock http_object.get to return a mock http response 3. mock http_response.body to return the contents of your file I think http_object has to have :null_object => true if you want to keep things simple. I ended up tossing ARes altogether, so I''m not actually using it at this point. But that''s at least pretty close to what I did. Ugly, but it worked. Pat
On 2/7/07, Pat Maddox <pergesu at gmail.com> wrote:> On 2/7/07, aslak hellesoy <aslak.hellesoy at gmail.com> wrote: > > On 2/7/07, Pat Maddox <pergesu at gmail.com> wrote: > > > I want to use ActiveResource in my app. Instead of hitting server > > > though, I want it to load from a file when I call find. Any clue how > > > I do that? > > > > > > > In the same way as you''d mock ActiveRecord I would think. Is this not > > working for you? > > No I want it to actually go through the process of finding something, > but just have it use a file rather than an actual server. >I see, so you don''t want to mock ActiveResource, but the transport used by it. This is an approach that I generally don''t recommend. The lower level you''re mocking at (HTTP, database connections, File I/O etc) the more you have to set up and things become very verbose and fragile. As a general rule: Don''t mock APIs you don''t own. Why don''t you just mock at a higher level - your ActiveResource classes? The ActiveResource classes themselves I would verify against the real transport, without mocking. The same goes for ActiveRecord. Their specs run against the database, but specs for anything *using* ActiveRecord/ActiveResource will talk to mock instances. Aslak> Takes a couple steps (with my current solution): > 1. Mock Net::HTTP.new to return some mock http object > 2. mock http_object.get to return a mock http response > 3. mock http_response.body to return the contents of your file > > I think http_object has to have :null_object => true if you want to > keep things simple. > > I ended up tossing ARes altogether, so I''m not actually using it at > this point. But that''s at least pretty close to what I did. Ugly, > but it worked. > > Pat > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On 2/7/07, aslak hellesoy <aslak.hellesoy at gmail.com> wrote:> On 2/7/07, Pat Maddox <pergesu at gmail.com> wrote: > > On 2/7/07, aslak hellesoy <aslak.hellesoy at gmail.com> wrote: > > > On 2/7/07, Pat Maddox <pergesu at gmail.com> wrote: > > > > I want to use ActiveResource in my app. Instead of hitting server > > > > though, I want it to load from a file when I call find. Any clue how > > > > I do that? > > > > > > > > > > In the same way as you''d mock ActiveRecord I would think. Is this not > > > working for you? > > > > No I want it to actually go through the process of finding something, > > but just have it use a file rather than an actual server. > > > > I see, so you don''t want to mock ActiveResource, but the transport used by it.Right> This is an approach that I generally don''t recommend. The lower level > you''re mocking at (HTTP, database connections, File I/O etc) the more > you have to set up and things become very verbose and fragile. As a > general rule: > > Don''t mock APIs you don''t own.I agree with you. This is certainly how I work with my AR classes. However there''s a big difference in maturity between AR and ARes. ARes isn''t even "official" yet. So while with AR I can mock MyClass.find and know that AR does the right thing, I''m not entirely sure with ARes. I don''t know how it''s going to parse an XML document. Turns out it did some funky things and I couldn''t use it. Once I had one spec that demonstrated ARes worked for my purposes, I''d just mock out all subsequent specs. Pat