2
\$\begingroup\$

Is the test flexible?

 describe "self.sort" do
 before(:each) do
 @tee = FactoryGirl.create :author, nickname: "tee jia hen", user: FactoryGirl.create(:user)
 @jon = FactoryGirl.create :author, nickname: "jon", user: FactoryGirl.create(:user)
 @tee_article1 = FactoryGirl.create :article, author: @tee, title: "3diablo"
 @tee_article2 = FactoryGirl.create :article, author: @tee, title: "1people"
 @jon_article = FactoryGirl.create :article, author:@jon, title: "2game"
 end
 it ", it should sort articles base on title" do
 Article.sort("title", "asc").should == [@tee_article2, @jon_article, @tee_article1]
 end
 end
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Aug 11, 2012 at 3:22
\$\endgroup\$
1
  • \$\begingroup\$ As far as I can tell, that is a perfectly well-written rspec test. But we can't decided if it's flexible until you tell us what you might do with it. \$\endgroup\$ Commented Aug 11, 2012 at 6:47

1 Answer 1

3
\$\begingroup\$

Provided you sort the active record results (as you have done), comparing to an array works fine. I'm not sure what you mean by 'flexible', but the test you've written looks pretty good (without knowing the internals of your 'sort' function).

I'd only suggest adding some more tests to cover reverse sorting, sorting by author.nickname (if that's part of the default scope, or pulled in via delegation), and passing invalid values to the sort function.

Being a little pedantic, you don't need to repeat the word "it" in the spec declaration. The word "it" will be prepended to the message in the case of failures.

 describe "self.sort" do
 before(:each) do
 @tee = FactoryGirl.create :author, nickname: "tee jia hen", user: FactoryGirl.create(:user)
 @jon = FactoryGirl.create :author, nickname: "jon", user: FactoryGirl.create(:user)
 @tee_article1 = FactoryGirl.create :article, author: @tee, title: "3diablo"
 @tee_article2 = FactoryGirl.create :article, author: @tee, title: "1people"
 @jon_article = FactoryGirl.create :article, author:@jon, title: "2game"
 end
 it "should sort articles base on title" do
 Article.sort("title", "asc").should == [@tee_article2, @jon_article, @tee_article1]
 end
 it "should reverse-sort articles base on title" do
 Article.sort("title", "desc").should == [@tee_article1, @jon_article, @tee_article2]
 end
 end
answered Aug 23, 2012 at 5:24
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.