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
\$\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\$
    – bkconrad
    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
\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.