Getting Class in Shared Behaviours

Hi,

I want to be able to get at the described class in my shared behaviour.
I’m
sure an example will say it better than my words

describe “my shared”, :shared => true do

it “should tell me what the class is its describing” do
how_do_i_get_the_user_class_here
end

end

describe User do
it_should_behave_like “my shared”

#…
end

So in my shared behaviour, how do I get access to the User class?

Cheers
Daniel

Did you try self.class

??
Nathan S.
[email protected]
rspec edge revision 2910
rspec_on_rails edge revision 2909
rails edge revision 8175

yep

should == User
expected: User,
got: #Class:0x25d0ce4 (using ==)

On Nov 21, 2007 3:14 PM, Daniel N [email protected] wrote:

end

describe User do
it_should_behave_like “my shared”

#…
end

So in my shared behaviour, how do I get access to the User class?

There’s no way to do this implicitly. i.e. rspec does not expose the
class. You’d have to have a method like described_class or something:

describe “my shared”, :shared => true do

it “should tell me what the class is its describing” do
described_class.should do_something_I_care_about
end

end

describe User do
def described_class
User
end

end

On Nov 21, 2007 10:22 PM, David C. [email protected] wrote:

how_do_i_get_the_user_class_here

So in my shared behaviour, how do I get access to the User class?
end

describe User do
def described_class
User
end

end

However, if you do this:

describe MyModule do # MyModule has a #hello method
it “should be polite” do
hello.should == ‘How do you do’
end
end

Modules are automatically mixed into your examples

Aslak

On Nov 22, 2007 8:31 AM, aslak hellesoy [email protected]
wrote:

end
end

Aslak

Cheers
Daniel
http://rubyforge.org/mailman/listinfo/rspec-users

Aslak

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.

Thanx again.
Daniel

David to the rescue! :slight_smile:

Nathan S.
[email protected]
rspec edge revision 2910
rspec_on_rails edge revision 2909
rails edge revision 8175

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?

Nathan S.
[email protected]
rspec edge revision 2910
rspec_on_rails edge revision 2909
rails edge revision 8175

On Nov 21, 2007 3:53 PM, Scott T. [email protected]
wrote:

it “should tell me what the class is its describing” do
end
before :each do

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?

Yes. You haven’t submitted it.

On Nov 21, 2007, at 4:22 PM, David C. wrote:

how_do_i_get_the_user_class_here

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?

Scott

On Nov 21, 2007 4:16 PM, Nathan S. [email protected] wrote:

Nathan S.
[email protected]
rspec edge revision 2910
rspec_on_rails edge revision 2909
rails edge revision 8175

Have you added this to your signature???

Yeah, I’m currently doing it manually though, and this email is only
for mailing lists.

Nathan S.
[email protected]
rspec edge revision 2910
rspec_on_rails edge revision 2909
rails edge revision 8175

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?

Nathan S.
[email protected]
rspec edge revision 2910
rspec_on_rails edge revision 2909
rails edge revision 8175

On Nov 21, 2007, at 5:31 PM, David C. wrote:

On Nov 21, 2007 4:16 PM, Nathan S. [email protected]
wrote:

Nathan S.
[email protected]
rspec edge revision 2910
rspec_on_rails edge revision 2909
rails edge revision 8175

Have you added this to your signature???

Haha. Probably not a bad option.

On Nov 21, 2007, at 5:16 PM, Nathan S. wrote:

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?

Go for it. Let me know if you don’t want it.

What is the syntax your thinking of?

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.

Nathan S.
[email protected]
rspec edge revision 2910
rspec_on_rails edge revision 2909
rails edge revision 8175

Not even sure, what are your thoughts?

Nathan S.
[email protected]
rspec edge revision 2910
rspec_on_rails edge revision 2909
rails edge revision 8175

On Nov 21, 2007, at 5:42 PM, Nathan S. wrote:

Not even sure, what are your thoughts?

Nathan S.
[email protected]
rspec edge revision 2910
rspec_on_rails edge revision 2909
rails edge revision 8175

I’d like to see something like this:

it_should_behave_like “a foo”, :variables => {
:bar => “bar”,
:baz => “baz”,
:class => Object
}

describe “a foo”, :shared => true do

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.

Scott

I like what Scott suggested.

it_should_behave_like “a foo”, :with => { … }

Might read a little better. But I like the idea of it just taking a
hash.

-Ben

Anyone else have any opinions on this? I’d like to get some more input.

Thanks,

Nathan S.
[email protected]
rspec edge revision 2910
rspec_on_rails edge revision 2909
rails edge revision 8175