I have something like:
class Foo
def initialize(template)
@template = template
end
def link
link_to “foo”, foo_path
end
def method_missing(*args, &block)
@template.send(*args, &block)
end
end
…
before(:all) do
@foo = Foo.new(self)
end
it “should have a link” do
@foo.link.should match /href/
end
… But I get this error:
NoMethodError in ‘FooRenderer should have a link’
undefined method `foo_path’ for
#ActiveSupport::TestCase::Subclass_1:0x105619710
… What can I include in my spec to get foo_path to be recognized?
Thank you.
Patrick J. Collins
http://collinatorstudios.com
On May 2, 2010, at 1:48 AM, Patrick J. Collins wrote:
end
… What can I include in my spec to get foo_path to be recognized?
Whate versions of ruby, rspec, and rails are you using? Please make it a
habit to include this information in queries like this.
Whate versions of ruby, rspec, and rails are you using? Please make it a habit to include this information in queries like this.
ruby 1.8.7
rails 2.3.5
rspec 1.3.0
Patrick J. Collins
http://collinatorstudios.com
This is a pattern that I believe pre-dates mock objects, and is rarely
implemented these days because mocks allow us to better separate things.
Hi David,
As I am still quite new to rspec, my way of working has been purely
based off
of my own best guess of how to do something… This is the reason why I
am
seeking some conversation and guidance from an Rspec expert (still am
waiting
to make that happen btw). Anyway, as I am a bit fuzzy on this, would
you
please give me a quick demonstration of how you would structure a test
for
/href/ that would utilize a more modern approach?
Thank you.
Patrick J. Collins
http://collinatorstudios.com
On Sun, May 2, 2010 at 1:48 AM, Patrick J. Collins
[email protected] wrote:
end
def method_missing(*args, &block)
@template.send(*args, &block)
end
end
…
before(:all) do
@foo = Foo.new(self)
end
Generally speaking, before(:each) is a better choice than
before(:all). Each example gets run in its own instance, and using
before(:all) risks leaking state from example to example.
The pattern that you’re implementing here is called the Self-Shunt
Pattern, in which the example itself acts like a player in the code
being tested. This is usually done to instrument the code so the
example can observe calls made to other objects (like a mock). This is
a pattern that I believe pre-dates mock objects, and is rarely
implemented these days because mocks allow us to better separate
things.
Of course, this is a bit different from what’s going on here. Here the
example is being used as a delegate to handle services not available
to the object. Named routes are made available to controller, view,
and helper specs, so you can get them by either storing this file in
any of those folders (spec/helpers, for example).
HTH,
David