[rspec-users] Question About Cleaning Up Specs?

Phillip Koebbe phillipkoebbe at gmail.com
Thu Feb 18 12:13:18 EST 2010


Joaquin Rivera Padron wrote:
> those who knows more say the idea is to keep in the example only what 
> is important for the example (but they surely say it even better than 
> me) which is what you are doing +1,
>
> greetings,
> joaquin
>
> 2010/2/18 Matt Wynne <matt at mattwynne.net <mailto:matt at mattwynne.net>>
>
>
>     On 18 Feb 2010, at 16:25, me at franklakatos.com
>     <mailto:me at franklakatos.com> wrote:
>
>         I know we are suppose to keep specs obvious and readable, but
>         is it normal (or there any opposition) to stubbing every
>         method call that you needed in a before block and simply
>         over-riding the same method with a should_receive in the
>         actual tested it()?
>
>         Example
>
>         describe "#create" do
>          it "should find a user"
>           @user = mock_model(User)
>           User.should_receive(:find).and_return @user
>           @posts = []
>           @post = mock_model(Post)
>           @posts.stub(:build).and_return @post
>           post "create"
>          end
>
>          it "should build a post from that user" do
>           @user = mock_model(User)
>           User.stub(:find).and_return @user
>           @posts = []
>           @post = mock_model(Post)
>           @posts.should_receive(:build).and_return @post
>           post "create"
>          end
>         end
>
>         ... would become ..
>
>
>         describe "#create" do
>          before do
>           @user = mock_model(User)
>           User.stub(:find).and_return @user
>           @posts = []
>           @post = mock_model(Post)
>           @posts.stub(:build).and_return @post
>          end
>
>          it "should find a user"
>           User.should_receive(:find).and_return @user
>           post "create"
>          end
>
>          it "should build a post from that user" do
>           @posts.should_receive(:build).and_return @post
>           post "create"
>          end
>         end
>

I often have a before :each that initializes objects to some default 
state and then change whatever is necessary in the individual examples. 
+1 here as well.

Peace,
Phillip


More information about the rspec-users mailing list