[rspec-users] need some advice on the best way to structure testable shared methods
David Chelimsky
dchelimsky at gmail.com
Tue Nov 15 07:18:18 EST 2011
On Nov 14, 2011, at 11:59 PM, Patrick J. Collins wrote:
>> Use the described class:
>>
>> shared_examples "a nameable thingie" do |klass|
>> describe "#build_name" do
>> it "it adds the collection of arguments to the base components and formats them for a form element name attribute" do
>> described_class.new(nil,nil).build_name(:lol, :lollerskates, :roflcopter).should == "lol[lollerskates][roflcopter]"
>> end
>> end
>> end
>
> But what happens if the classes do not have the same argument expectations,
> such as:
>
> class Foo
>
> include NameBuilder
>
> def initialize(arg1)
> ...
> end
>
> end
I misunderstood your initial example, thinking that NameBuilder required an initializer w/ two args.
I'd use a factory pattern here:
def new_foo(arg1=nil,arg2=nil)
Foo.new(arg1,arg2)
end
def new_bar(arg=nil)
Bar.new(arg)
end
Now you can use these without arguments when you want to:
describe Foo do
it_behaves_like "a nameable thingie", new_foo
end
describe Bar do
it_behaves_like "a nameable thingie", new_bar
end
And you can use the with arguments as well:
it "does something else" do
foo = make_foo(x)
other_foo = make_foo(x,y)
end
David
More information about the rspec-users
mailing list