# Stylistic preferences

**URL:** <https://www.ruby-forum.com/t/stylistic-preferences/121620>\
**Category:** RSpec\
**Created:** [November 29, 2007, 11:56am UTC](https://www.ruby-forum.com/t/stylistic-preferences/121620 "2007-11-29T11:56:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![datorrey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cab0a1/32.png) [@datorrey](https://www.ruby-forum.com/u/datorrey)\
**Post date:** [November 29, 2007, 11:56am UTC](https://www.ruby-forum.com/t/stylistic-preferences/121620/1 "2007-11-29T11:56:17Z")

</div>

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

* * *

1. 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

---

<div class="post-metadata">

**Author:** ![datorrey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cab0a1/32.png) [@datorrey](https://www.ruby-forum.com/u/datorrey)\
**Post date:** [December 3, 2007, 2:40pm UTC](https://www.ruby-forum.com/t/stylistic-preferences/121620/2 "2007-12-03T14:40:46Z")

</div>

On Nov 29, 2007, at 5:54 AM, Daniel T. wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![datorrey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cab0a1/32.png) [@datorrey](https://www.ruby-forum.com/u/datorrey)\
**Post date:** [December 3, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/stylistic-preferences/121620/3 "2007-12-03T14:46:04Z")

</div>

The code works fine. I was asking about the “given” thing.

Daniel

---

<div class="post-metadata">

**Author:** ![datorrey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cab0a1/32.png) [@datorrey](https://www.ruby-forum.com/u/datorrey)\
**Post date:** [December 3, 2007, 4:33pm UTC](https://www.ruby-forum.com/t/stylistic-preferences/121620/4 "2007-12-03T16:33:38Z")

</div>

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 T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
