Thanx. I was aware of that behaviour, but this module is being mixed
into
AR classes and relies on there being methods available to AR models. I
really want to implemnet these specs as a shared behaviour on each
implementing model.
That way I can check to make sure that the model has the correct
attributes
for the mixin to function properly etc.
Some of this clears up my issues around sharing behaviors that you can
pass parameters to. I was doing a hackish solution before by
including a module then calling a method it provides, but now I can
have shared behaviors and just have them call specific methods which
then become a convention to define. It still feels hackish, but not
nearly as much. Is there another way?
So in my shared behaviour, how do I get access to the User class?
end
describe User do
def described_class
User
end
…
end
Or you could just set up instance variables in your before :each block:
describe “an object which has to_s”, :shared => true do
it “should work!” do
:foo.send(@method).should == “foo”
end
end
describe Symbol do
before :each do @method = :to_s
end
it_should_behave_like “an object which has to_s”
end
On another note, I’ve been poking around Rubinius’ source, which uses
a scaled down version of rspec, and they already have shared examples
with parameters:
shared :symbol_id2name do |cmd|
describe “Symbol##{cmd}” do
it “returns the string corresponding to self” do
:rubinius.send(cmd).should == “rubinius”
:squash.send(cmd).should == “squash”
:[].send(cmd).should == “[]”
:@ruby.send(cmd).should == “@ruby”
:@@ruby.send(cmd).should == “@@ruby”
end
end
end
require File.dirname(FILE) + ‘/…/…/spec_helper’
describe “Symbol#to_s” do
it_behaves_like(:symbol_id2name, :to_s)
end
This doesn’t seem that hard to implement. Is there some reason a
patch has been created yet?
Ooh, I totally want to do this, I’ll work on it this week along with
my other patch i have yet to submit this week, unless Scott is partial
to doing it. Do you want it, Scott?
Ooh, I totally want to do this, I’ll work on it this week along with
my other patch i have yet to submit this week, unless Scott is partial
to doing it. Do you want it, Scott?
Oh, and the reason I include this is because it’s always a question
when discussing things, and this makes it always available, both to
those reading now and those who may read these conversations in the
future.
it “should have the variable bar there, equal to bar” do
bar.should == “bar”
end
end
Conceivably, you could do some metaprogramming to define methods
“bar” and “baz” in the ExampleGroupClass (or whatever that thing is
called now) to return the values give in the hash.