What are people''s opinions on which of these two styles is better to use? 1) before --------------------------- module UserSpecHelper include GenericSpecHelper def valid_sms_attributes(phone_number="12345") { :phone_number => phone_number } end end describe User, "with phone number" do include UserSpecHelper before(:each) do @user = User.new(valid_sms_attributes) end it "should be valid" do @user.should be_valid end it "should reject duplicate phone number" do @user.save @user_2 = User.new(valid_sms_attributes) @user_2.should_not be_valid end it "should be possible to disable the number" do @user.save @user.disable_number @user.should be_disabled end end ------------------------ 2) given/yield --------------------------- module GenericSpecHelper def given(thing) yield thing if block_given? thing end end module UserSpecHelper include GenericSpecHelper def valid_sms_attributes(phone_number="12345") { :phone_number => phone_number } end def valid_sms_user(phone_number="12345") User.new(valid_sms_attributes(phone_number)) end end describe User, "unconfirmed" do include UserSpecHelper it "should be valid" do valid_sms_user.should be_valid end it "should reject duplicate phone number" do valid_sms_user("1").save valid_sms_user("1").should_not be_valid end it "should be possible to disable the number" do given(valid_sms_user) do |user| user.save user.disable_number user.should be_disabled end end end ------------------- My thoughts: the second style is more readable sometimes, less other times. More importantly, with the first style, my specs tend to be split alongside the lines of whether they can use the same before (:each), rather than whether they belong together, whereas with the second one, one "description" can have several different "starting points", and I group them by whether I feel they belong together logically. So at the moment I''m more comfortable with the second style. What do you think? Daniel PS: I know these specs themselves are trivial... I''ve used both approaches in less trivial specs, I hope these illustrate the idea though
On Nov 29, 2007, at 5:54 AM, Daniel Tenner wrote:> What are people''s opinions on which of these two styles is better to > use? > > > it "should be possible to disable the number" do > given(valid_sms_user) do |user| > user.save > user.disable_number > user.should be_disabled > end > end >it "should be possible to disable the number" do lambda { user.save user.disable_number }.should change(user, :disabled?).from(true).to(false) end If this doesn''t work, the solution is very close to this.
The code works fine. I was asking about the "given" thing. Daniel On 3 Dec 2007, at 13:32 3 Dec 2007, Bryan Liles wrote:> > On Nov 29, 2007, at 5:54 AM, Daniel Tenner wrote: > >> What are people''s opinions on which of these two styles is better to >> use? >> >> >> it "should be possible to disable the number" do >> given(valid_sms_user) do |user| >> user.save >> user.disable_number >> user.should be_disabled >> end >> end >> > > > it "should be possible to disable the number" do > lambda { > user.save > user.disable_number > }.should change(user, :disabled?).from(true).to(false) > end > > If this doesn''t work, the solution is very close to this. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Well, I kinda like the word given, however I feel it''s competing with the Context in describe Class, "Context" do Maybe before should be replaced with some givens and expectations, like so: describe Class, "Context" do given do end always_expect do end it "should ..." do end end Stefan 2007/11/29, Daniel Tenner <daniel.ruby at tenner.org>:> > What are people''s opinions on which of these two styles is better to > use? > > 1) before > --------------------------- > module UserSpecHelper > include GenericSpecHelper > > def valid_sms_attributes(phone_number="12345") > { :phone_number => phone_number } > end > end > > describe User, "with phone number" do > include UserSpecHelper > > before(:each) do > @user = User.new(valid_sms_attributes) > end > > it "should be valid" do > @user.should be_valid > end > > it "should reject duplicate phone number" do > @user.save > @user_2 = User.new(valid_sms_attributes) > @user_2.should_not be_valid > end > > it "should be possible to disable the number" do > @user.save > @user.disable_number > @user.should be_disabled > end > end > > > ------------------------ > 2) given/yield > --------------------------- > module GenericSpecHelper > def given(thing) > yield thing if block_given? > thing > end > > end > > > module UserSpecHelper > include GenericSpecHelper > def valid_sms_attributes(phone_number="12345") > { :phone_number => phone_number } > end > > def valid_sms_user(phone_number="12345") > User.new(valid_sms_attributes(phone_number)) > end > end > > > describe User, "unconfirmed" do > include UserSpecHelper > > it "should be valid" do > valid_sms_user.should be_valid > end > > it "should reject duplicate phone number" do > valid_sms_user("1").save > valid_sms_user("1").should_not be_valid > end > > it "should be possible to disable the number" do > given(valid_sms_user) do |user| > user.save > user.disable_number > user.should be_disabled > end > end > > > end > ------------------- > > My thoughts: the second style is more readable sometimes, less other > times. More importantly, with the first style, my specs tend to be > split alongside the lines of whether they can use the same before > (:each), rather than whether they belong together, whereas with the > second one, one "description" can have several different "starting > points", and I group them by whether I feel they belong together > logically. So at the moment I''m more comfortable with the second style. > > What do you think? > > Daniel > > PS: I know these specs themselves are trivial... I''ve used both > approaches in less trivial specs, I hope these illustrate the idea > though > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- Bekk Open Source http://boss.bekk.no -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071203/8f82b69b/attachment.html