Well, I kinda like the word given, however I feel it's competing with the Context in describe Class, "Context" do<br><br>Maybe before should be replaced with some givens and expectations, like so:<br><br>describe Class, "Context" do
<br><br> given do<br><br> end<br><br> always_expect do<br><br> end<br><br> it "should ..." do <br><br> end<br><br>end<br><br>Stefan<br><br><div><span class="gmail_quote">2007/11/29, Daniel Tenner <<a href="mailto:daniel.ruby@tenner.org">
daniel.ruby@tenner.org</a>>:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">What are people's opinions on which of these two styles is better to
<br>use?<br><br>1) before<br>---------------------------<br>module UserSpecHelper<br> include GenericSpecHelper<br><br> def valid_sms_attributes(phone_number="12345")<br> { :phone_number => phone_number }
<br> end<br>end<br><br>describe User, "with phone number" do<br> include UserSpecHelper<br><br> before(:each) do<br> @user = User.new(valid_sms_attributes)<br> end<br><br> it "should be valid" do
<br> @user.should be_valid<br> end<br><br> it "should reject duplicate phone number" do<br> @user.save<br> @user_2 = User.new(valid_sms_attributes)<br> @user_2.should_not be_valid<br> end<br>
<br> it "should be possible to disable the number" do<br> @user.save<br> @user.disable_number<br> @user.should be_disabled<br> end<br>end<br><br><br>------------------------<br>2) given/yield<br>
---------------------------<br>module GenericSpecHelper<br> def given(thing)<br> yield thing if block_given?<br> thing<br> end<br><br>end<br><br><br>module UserSpecHelper<br> include GenericSpecHelper<br> def valid_sms_attributes(phone_number="12345")
<br> { :phone_number => phone_number }<br> end<br><br> def valid_sms_user(phone_number="12345")<br> User.new(valid_sms_attributes(phone_number))<br> end<br>end<br><br><br>describe User, "unconfirmed" do
<br> include UserSpecHelper<br><br> it "should be valid" do<br> valid_sms_user.should be_valid<br> end<br><br> it "should reject duplicate phone number" do<br> valid_sms_user("1").save
<br> valid_sms_user("1").should_not be_valid<br> end<br><br> it "should be possible to disable the number" do<br> given(valid_sms_user) do |user|<br> user.save<br> user.disable_number
<br> user.should be_disabled<br> end<br> end<br><br><br>end<br>-------------------<br><br>My thoughts: the second style is more readable sometimes, less other<br>times. More importantly, with the first style, my specs tend to be
<br>split alongside the lines of whether they can use the same before<br>(:each), rather than whether they belong together, whereas with the<br>second one, one "description" can have several different "starting
<br>points", and I group them by whether I feel they belong together<br>logically. So at the moment I'm more comfortable with the second style.<br><br>What do you think?<br><br>Daniel<br><br>PS: I know these specs themselves are trivial... I've used both
<br>approaches in less trivial specs, I hope these illustrate the idea<br>though<br>_______________________________________________<br>rspec-users mailing list<br><a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org
</a><br><a href="http://rubyforge.org/mailman/listinfo/rspec-users">http://rubyforge.org/mailman/listinfo/rspec-users</a><br></blockquote></div><br><br clear="all"><br>-- <br>Bekk Open Source<br><a href="http://boss.bekk.no">
http://boss.bekk.no</a>