10 ms·
RSpec Best Practices
- JangoSteve 16y agoThis was a good article, but only covered the more superficial best practices concerning formatting. The more important best practices include things like, you don't need to save an object to test its validity. I see this a lot: it "returns no errors for a valid object" do @object.save @object.errors.size.should == 0 end This doesn't scale well and will cause your specs to run painfully slow when your project gets bigger, as the spec will hit the database every time it's run. Instead, do this: it "returns no errors for a valid object" do @object.valid? @object.errors.size.should == 0 end Or even better: it "returns no errors for a valid object" do @object.should be_valid end Neither of these actually hit the database, so the entire thing is run in-memory. And this is just one example of functional RSpec best practices. Don't get me wrong, the formatting stuff definitely helps readability across the team, but if your specs are testing the wrong thing, or running slowly, the formatting doesn't matter.