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
-
\$\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– bkconrad2012年08月11日 06:47:13 +00:00Commented Aug 11, 2012 at 6:47
1 Answer 1
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