Hi! I''m new to rspec and was wondering how named_scopes are usually tested? Is it enough to test that it''s defined? or do you need to test the behavior as well? I''ve been reading around and this seems to be the tester''s choice, i just want to get people''s opinion on this :D
On Sun, Jan 17, 2010 at 8:17 PM, Nin <npdepolonia at gmail.com> wrote:> Hi! I''m new to rspec and was wondering how named_scopes are usually > tested? Is it enough to test that it''s defined? or do you need to test > the behavior as well? I''ve been reading around and this seems to be > the tester''s choice, i just want to get people''s opinion on this :D >_Specify_ the behaviour, don''t _test_ the implementation. The fact that a method is defined with a named_scope declaration is irrelevant to the behaviour. One reason for this is keeping things decoupled. Consider the fact that named_scope just changed to scope today [1]. If your specs specify a call to named_scope, they''ll have to change, whereas if they they only specify the name you define then you''ll only need to update the implementation when you upgrade. [1] http://github.com/rails/rails/commit/d60bb0a9e4be2ac0a9de9a69041a4ddc2e0cc914 That all make sense? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100117/0988f69a/attachment.html>
class User < ActiveRecord::Base named_scope :admins, :conditions => {:admin => true} end describe User, "admins" do it "should include users with admin flag" do admin = User.create! :admin => true User.admin.should include(admin) end it "should not include users without admin flag" do admin = User.create! :admin => false User.admin.should_not include(admin) end end On Jan 17, 2010, at 6:17 PM, Nin wrote:> Hi! I''m new to rspec and was wondering how named_scopes are usually > tested? Is it enough to test that it''s defined? or do you need to test > the behavior as well? I''ve been reading around and this seems to be > the tester''s choice, i just want to get people''s opinion on this :D > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
On Mon, Jan 18, 2010 at 4:01 AM, Pat Maddox <mailinglists at patmaddox.com> wrote:> On Jan 17, 2010, at 6:17 PM, Nin wrote: > > Hi! I''m new to rspec and was wondering how named_scopes are usually > > tested? Is it enough to test that it''s defined? or do you need to test > > the behavior as well? I''ve been reading around and this seems to be > > the tester''s choice, i just want to get people''s opinion on this :D > > class User < ActiveRecord::Base > ?named_scope :admins, :conditions => {:admin => true} > end > > describe User, "admins" do > ?it "should include users with admin flag" do > ? ?admin = User.create! :admin => true > ? ?User.admin.should include(admin) > ?end > > ?it "should not include users without admin flag" do > ? ?admin = User.create! :admin => false > ? ?User.admin.should_not include(admin) > ?end > endSmall style matter, but I''ve leaning towards more declarative sounding example names: describe User, ".admins" do ??it "includes users with admin flag" do ?? ?admin = User.create! :admin => true ?? ?User.admin.should include(admin) ??end ??it "excludes users without admin flag" do ?? ?non_admin = User.create! :admin => false ?? ?User.admin.should_not include(non_admin) ??end end class User < ActiveRecord::Base ??named_scope :admins, :conditions => {:admin => true} end We still have ''should'' in the examples, but this produces more ''spec-like'' output: User.admins ??includes users with admin flag ??excludes users without admin flag FWIW, David
On Mon, Jan 18, 2010 at 5:17 AM, David Chelimsky <dchelimsky at gmail.com>wrote:> On Mon, Jan 18, 2010 at 4:01 AM, Pat Maddox <mailinglists at patmaddox.com> > wrote: >...> > class User < ActiveRecord::Base > > named_scope :admins, :conditions => {:admin => true} > > end > > > > describe User, "admins" do > > it "should include users with admin flag" do > > admin = User.create! :admin => true > > User.admin.should include(admin) > > end > > > > it "should not include users without admin flag" do > > admin = User.create! :admin => false > > User.admin.should_not include(admin) > > end > > end > > Small style matter, but I''ve leaning towards more declarative sounding > example names: > > describe User, ".admins" do > it "includes users with admin flag" do > admin = User.create! :admin => true > User.admin.should include(admin) > end > > it "excludes users without admin flag" do > non_admin = User.create! :admin => false > User.admin.should_not include(non_admin) > end > end > > class User < ActiveRecord::Base > named_scope :admins, :conditions => {:admin => true} > end > > We still have ''should'' in the examples, but this produces more > ''spec-like'' output: > > User.admins > includes users with admin flag > excludes users without admin flagI agree, David. I''ve been omitting "should" from the beginning. For me, it made the start of every example look too similar. Regards, Craig -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100118/37b11a90/attachment-0001.html>
On Sun, Jan 17, 2010 at 9:17 PM, Nin <npdepolonia at gmail.com> wrote:> Hi! I''m new to rspec and was wondering how named_scopes are usually > tested? Is it enough to test that it''s defined? or do you need to test > the behavior as well? I''ve been reading around and this seems to be > the tester''s choice, i just want to get people''s opinion on this :DWhile this is focused on shoulda, I still found it helpful for demonstrating how to properly deal with testing named_scopes. http://robots.thoughtbot.com/post/200254501/testing-named-scopes Best regards, Michael Guterl
David Chelimsky wrote:> On Mon, Jan 18, 2010 at 4:01 AM, Pat Maddox <mailinglists at patmaddox.com> wrote: > >> On Jan 17, 2010, at 6:17 PM, Nin wrote: >> >>> Hi! I''m new to rspec and was wondering how named_scopes are usually >>> tested? Is it enough to test that it''s defined? or do you need to test >>> the behavior as well? I''ve been reading around and this seems to be >>> the tester''s choice, i just want to get people''s opinion on this :D >>> >> class User < ActiveRecord::Base >> named_scope :admins, :conditions => {:admin => true} >> end >> >> describe User, "admins" do >> it "should include users with admin flag" do >> admin = User.create! :admin => true >> User.admin.should include(admin) >> end >> >> it "should not include users without admin flag" do >> admin = User.create! :admin => false >> User.admin.should_not include(admin) >> end >> end >> > > Small style matter, but I''ve leaning towards more declarative sounding > example names: > > describe User, ".admins" do > it "includes users with admin flag" do > admin = User.create! :admin => true > User.admin.should include(admin) > end > > it "excludes users without admin flag" do > non_admin = User.create! :admin => false > User.admin.should_not include(non_admin) > end > end > > class User < ActiveRecord::Base > named_scope :admins, :conditions => {:admin => true} > end > > We still have ''should'' in the examples, but this produces more > ''spec-like'' output: > > User.admins > includes users with admin flag > excludes users without admin flag > > FWIW, > David >Another small style matter.. I like using the :: notation for class methods as that is what the documentation tools tend to use (RDoc and Yard): describe User, "::admins" do ... ... end On the topic of RSpec as a form of documentation has anyone used the yard-doc rspec plugin? It appears to be pretty limited ATM but seems very cool with a lot of potential. Just like Ioke''s docs it embeds the specs as part of the documentation with the option to view the source. Here is the example from the projects home page: http://lsegal.github.com/yard-spec-plugin/String.html#pig_latin-instance_method -Ben
Nin wrote:> Hi! I''m new to rspec and was wondering how named_scopes are usually > tested? Is it enough to test that it''s defined? or do you need to test > the behavior as well? I''ve been reading around and this seems to be > the tester''s choice, i just want to get people''s opinion on this :DHere is my approach: http://gusiev.com/2010/07/bdd-rspec-matcher-to-test-named_scope-scoped-rails-3/ It is a little advanced. Hopefully you will get the idea. -- Posted via http://www.ruby-forum.com/.