Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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
share|improve this question
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. – bkconrad Aug 11 '12 at 6:47

1 Answer

up vote 2 down vote accepted

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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