Cameron Booth
2008-Oct-28 12:21 UTC
[rspec-users] Accessing the model class from within an rspec-rails spec
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
Cameron Booth
2008-Oct-28 13:09 UTC
[rspec-users] Accessing the model class from within an rspec-rails spec
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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081028/7b231568/attachment.html>
Cameron Booth
2008-Oct-28 13:40 UTC
[rspec-users] Accessing the model class from within an rspec-rails spec
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? 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-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081028/a8db880e/attachment.html>
David Chelimsky
2008-Oct-29 04:46 UTC
[rspec-users] Accessing the model class from within an rspec-rails spec
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!!You''re looking for example_group.described_type, which you can get to like this: describe Foo do described_type # =>Foo it "should be Foo" do self.class.described_type # => Foo end end Cheers, David> > Cameron > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Pat Maddox
2008-Oct-29 05:11 UTC
[rspec-users] Accessing the model class from within an rspec-rails spec
"Cameron Booth" <cameron.booth at gmail.com> writes:> Is there a way to access the class itself that I''m missing?described_type Pat
Scott Taylor
2008-Oct-29 05:40 UTC
[rspec-users] Accessing the model class from within an rspec-rails spec
On Oct 28, 2008, at 9:09 AM, Cameron Booth 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!!Why not something like this: before(:each) { @user = User.new } it { @user.validate_presence_of(:name) } Scott
Mike Gunderloy
2008-Oct-29 11:23 UTC
[rspec-users] Accessing the model class from within an rspec-rails spec
Part of the testing harness on one of our projects: module Spec::Example::ExampleGroupMethods def model self.described_type.to_s.underscore end def should_require(*attrs) raise "should require needs at least one attribute" if attrs.empty? model = model() attrs.each do |attribute| it "should require :#{attribute}" do m = Factory.build(model.to_sym) m.send("#{attribute}=", nil) m.should have(1).error_on(attribute) end end end ... end Using FactoryGirl for spinning up instances, but of course easily tweaked for other mocking. Mike On Oct 28, 2008, at 8:09 AM, Cameron Booth 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
Cameron Booth
2008-Oct-29 15:00 UTC
[rspec-users] Accessing the model class from within an rspec-rails spec
Hi everybody, David and Pat, thanks for the tip on using "described_type" to access the model class. I actually figured that one out like 10 minutes after sending the email. 2 learnings there, first, sometimes explaining the problem helps solve the problem, and second, maybe I should sit on those emails for 10 minutes before sending them ;-) Scott, to answer your question, basically I''d love to find something less verbose. I''m really liking rspec, but am finding my specs are getting longer and longer, and I''m basically in the process of investigating what options are out there in terms of shortening the commonly done stuff in specs (specifically in the context of Rails). Technoweenie has done some stuff on this, as has awfreeman on github. I''m in the process of diving deeper into those concepts to maybe pull together a plugin or something, will let this group know. Cheers! Cameron> Message: 6 > Date: Wed, 29 Oct 2008 01:40:08 -0400 > From: Scott Taylor <mailing_lists at railsnewbie.com> > Subject: Re: [rspec-users] Accessing the model class from within an > rspec-rails spec > To: rspec-users <rspec-users at rubyforge.org> > Message-ID: <3961A8F4-311D-4237-B82D-36EFF0D56FAE at railsnewbie.com> > Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes > > > On Oct 28, 2008, at 9:09 AM, Cameron Booth 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!! > > Why not something like this: > > before(:each) { @user = User.new } > > it { @user.validate_presence_of(:name) } > > Scott > > > > ------------------------------ > > Message: 7 > Date: Wed, 29 Oct 2008 01:15:43 -0400 > From: "Stephen Eley" <sfeley at gmail.com> > Subject: Re: [rspec-users] Should acceptance tests be run against a > production environment? > To: rspec-users <rspec-users at rubyforge.org> > Message-ID: > <1fb4df0810282215u1c8552dag5f96fd86abdf6802 at mail.gmail.com> > Content-Type: text/plain; charset=ISO-8859-1 > > On Tue, Oct 28, 2008 at 1:08 PM, Pat Maddox <pergesu at gmail.com> wrote: > > When you do end-to-end acceptance testing with Selenium, I think it > > should be run against a production environment. Not THE production > > environment, mind you, but simply a new Rails app running with > > RAILS_ENV=production. > > I believe that''s what the angels call a "staging environment." > > And yes. > > -- > Have Fun, > Steve Eley (sfeley at gmail.com) > ESCAPE POD - The Science Fiction Podcast Magazine > http://www.escapepod.org > > > ------------------------------ > > Message: 8 > Date: Tue, 28 Oct 2008 22:11:39 -0700 > From: Pat Maddox <pergesu at gmail.com> > Subject: Re: [rspec-users] Accessing the model class from within an > rspec-rails spec > To: rspec-users <rspec-users at rubyforge.org> > Message-ID: <m2abcoouic.fsf at gmail.com> > Content-Type: text/plain; charset=us-ascii > > "Cameron Booth" <cameron.booth at gmail.com> writes: > > > Is there a way to access the class itself that I''m missing? > > described_type > > Pat > > > ------------------------------ > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > End of rspec-users Digest, Vol 28, Issue 76 > ******************************************* >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081029/5ac6f2d7/attachment.html>
Nick Hoffman
2008-Oct-29 16:01 UTC
[rspec-users] Accessing the model class from within an rspec-rails spec
On 2008-10-28, at 09:09, Cameron Booth wrote:> 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 > endHi Cameron. I haven''t played with RSpec''s internals at all, but considering that #it_should_validate_presence_of is nested within describe User do I would imagine that there''s a way to grab "User". Have a look at the structure of the Example, ExampleGroup, etc classes. It''ll be stored in one of those, somewhere. -Nick
Zach Dennis
2008-Oct-29 21:05 UTC
[rspec-users] Accessing the model class from within an rspec-rails spec
On Wed, Oct 29, 2008 at 12:01 PM, Nick Hoffman <nick at deadorange.com> wrote:> On 2008-10-28, at 09:09, Cameron Booth wrote: > >> 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 >> > > Hi Cameron. I haven''t played with RSpec''s internals at all, but considering > that > #it_should_validate_presence_of > is nested within > describe User do > I would imagine that there''s a way to grab "User". Have a look at the > structure of the Example, ExampleGroup, etc classes. It''ll be stored in one > of those, somewhere.described_type is what you want. This represents the object being described, -- Zach Dennis http://www.continuousthinking.com http://www.mutuallyhuman.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081029/1aa1c079/attachment.html>
Pat Maddox
2008-Oct-29 21:26 UTC
[rspec-users] Accessing the model class from within an rspec-rails spec
Nick Hoffman <nick at deadorange.com> writes:> On 2008-10-28, at 09:09, Cameron Booth wrote: >> 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 > > Hi Cameron. I haven''t played with RSpec''s internals at all, but > considering that > #it_should_validate_presence_of > is nested within > describe User do > I would imagine that there''s a way to grab "User". Have a look at the > structure of the Example, ExampleGroup, etc classes. It''ll be stored > in one of those, somewhere.For some reason my emails are getting bounced or delayed or something, so I''ll try again in case my first one didn''t go through... You''re looking for described_type Pat
David Chelimsky
2008-Oct-30 03:43 UTC
[rspec-users] Accessing the model class from within an rspec-rails spec
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 >