Comments on: (My) RSpec best practices and tips http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/?utm_source=rss&utm_medium=rss&utm_campaign=my-rspec-best-practices-and-tips Simple and delicious. Sun, 01 Mar 2015 12:30:00 +0000 hourly 1 By: Phil http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/comment-page-1/#comment-22571 Sat, 16 Mar 2013 15:05:14 +0000 http://eggsonbread.com/?p=227#comment-22571 +1

Thanks for tips sharing!

]]>
By: Philippe Creux http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/comment-page-1/#comment-14904 Sun, 19 Aug 2012 10:56:46 +0000 http://eggsonbread.com/?p=227#comment-14904 Thanks Jack!

You can use a combination of subject and its for the output to make more sense.

subject { @obj }

its(:field1) { should == 37 }
its(:filed2) { should == 19 }

Also, if your object is an ActiveRecord model, you could use set which is available as a gem.

Cheers

]]>
By: Jack Repenning http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/comment-page-1/#comment-14814 Fri, 10 Aug 2012 02:03:11 +0000 http://eggsonbread.com/?p=227#comment-14814 All great stuff, made my life 10x better in 2 minutes!

I still have one thorny problem, though. Have any thoughts?

My spec is testing code that creates a large object, 110 fields, with a lot of complex interrelationships. With your help, I got to this form:

describe “the object” do
before(:all) do
@obj = expensively_create_the_object
end
it “should exist” { @obj.should_not == nil }
it “should have proper field1″ { @obj.field1.should == 37 }
it “should have proper field2″ { @obj.field2.should == 19 }

end

I tried

specify { @obj.field2.should == 37 }
specify { @obj.field2.should == 19 }

which is more readable, but the test output says I pass or fail stuff like:

should == nil
should == nil
should == 3
should == “Demo”
should not == nil
should == nil

Is there any way to get specify to name the tested field?

]]>
By: Andrey http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/comment-page-1/#comment-14195 Fri, 06 Jul 2012 08:46:57 +0000 http://eggsonbread.com/?p=227#comment-14195 Thank you for post!
In code:


context "when valid" do
it "should return 'January' for 1" # lower boundary
it "should return 'March' for 3"
it "should return 'December' for 12" # upper boundary

you missed end keyword.

]]>
By: Ryan Cheung http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/comment-page-1/#comment-13237 Wed, 13 Jun 2012 01:37:29 +0000 http://eggsonbread.com/?p=227#comment-13237 It has been 2 years since this post published. And it’s still very helpful for us. Thank you!

]]>
By: Jason Woodlee http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/comment-page-1/#comment-11167 Tue, 08 May 2012 15:23:02 +0000 http://eggsonbread.com/?p=227#comment-11167 awesome.. didnt know 1/2 of these tricks..

]]>
By: RAYMUNDO http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/comment-page-1/#comment-6539 Wed, 19 Oct 2011 10:16:06 +0000 http://eggsonbread.com/?p=227#comment-6539 Hello. This is a tweet. Using twitter API for ios

]]>
By: Marco Perrando http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/comment-page-1/#comment-5805 Mon, 22 Aug 2011 09:44:42 +0000 http://eggsonbread.com/?p=227#comment-5805 I’ve discovered that you can use “subject” to modify your object under test in the before blocks, avoiding the reduntant blocks in wich you define a @user and set it as the subject.
So you can shorten even more your test to

describe User do
context "when name empty" do
it { should not be_valid }
specify { subject.save.should == false }
end

context "when name not empty" do
before { subject.name = 'Sam' }

it { should be_valid }
specify { subject.save.should == true }
end

describe :present do
subject { subject.present }

context "when user is a W" do
before { subject.gender = 'W' }

it { should be_a Flower }
end

context "when user is a M" do
before { subject.gender = 'M' }

it { should be_an IMac }
end
end
end

]]>
By: Bogdan Gusiev http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/comment-page-1/#comment-5427 Wed, 20 Jul 2011 19:05:00 +0000 http://eggsonbread.com/?p=227#comment-5427 That is a good start with all set of rspec features. And in addition you may combine context, subject and let and receive completely new opportunities.

Good example of what you can do: http://gusiev.com/dorspec/#24

]]>
By: JDK http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/comment-page-1/#comment-5291 Mon, 11 Jul 2011 11:25:50 +0000 http://eggsonbread.com/?p=227#comment-5291 Very interesting things–learned a lot from this, so thanks! I’m left with a question though–what do you see wrong with verbosity? I’ve never had a problem with verbosity. My feeling is that if the code is verbose, without impacting performance, it is easier for other who have to deal with it to understand it.

]]>