[rspec-users] Accessing the model class from within an rspec-rails spec

David Chelimsky dchelimsky at gmail.com
Wed Oct 29 23:43:33 EDT 2008


On Tue, Oct 28, 2008 at 8:40 AM, Cameron Booth <cameron.booth at gmail.com> wrote:
> Hi again,
>
> I think I may have partly found my own answer.
>
> Looking at this gist http://gist.github.com/14050 by Andy Freeman, it seems
> to do much of what I'm looking for right now (thanks, awesome!!).
>
> But playing around with it a bit, calling self.described_type which I
> thought would return the class, it's not, it seems to be looking only for a
> module?
>
>       def described_type
>         description_parts.find {|part| part.is_a?(Module)}
>       end
>
> Is there something I'm missing?

Some example groups describe modules, some classes. RSpec supports
both, and Module does it:

$ irb
>> module Foo; end
=> nil
>> Foo.is_a?(Module)
=> true
>> class Bar; end
=> nil
>> Bar.is_a?(Module)
=> true

whereas Class does not.

HTH,
David
Cheers,
David






>
> Cameron
>
>
>
> On Tue, Oct 28, 2008 at 8:09 AM, Cameron Booth <cameron.booth at gmail.com>
> wrote:
>>
>> Hi everybody,
>>
>> New to the list, so apologies if this has been answered elsewhere, but I
>> didn't find it. I'm trying to build up a plugin of useful rspec macros for
>> rails development, eg. things like:
>>
>> it_should_return_success
>> it_should_redirect_to { some_url }
>>
>> I'm basing my ideas off of some stuff technoweenie has done, as well as a
>> few others.
>>
>> One thing I'd love to do is be able to figure out the model class in a
>> rails model spec, so I can do something like:
>>
>> describe User do
>>   it_should_validate_presence_of :name
>> end
>>
>> I can get it working if I pass in User as an argument:
>>
>> describe User do
>>   it_should_validate_presence_of User, :name
>> end
>>
>> but that feels redundant. Is there a way to access the class itself that
>> I'm missing? On the controller spec side, I see there is
>> controller_class_name, but that needs to be set with the controller_name
>> method. I could go for something like that if required, but somehow it seems
>> like it would be overkill.
>>
>> Any advice would be greatly appreciated, thanks!!
>>
>> Cameron
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list