Tests

Ruby code posted by joshuaborn
created at 11 Oct 23:23

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require 'test_helper'

class CategoryTest < ActiveSupport::TestCase

  should have_many(:articles)

  context "The Category class" do

    should "return a list of categories in order" do
      second = Factory(:category, :position => 2)
      first = Factory(:category, :position => 1)
      third = Factory(:category, :position => 3)

      assert_equal [first, second, third], Category.by_position
    end

    should "return a list of only those categories that have published articles" do
      used = Factory(:category)
      also_used = Factory(:category)
      unused = Factory(:category)
      unpublished = Factory(:category)

      3.times { Factory(:published_article, :category => used) }
      2.times { Factory(:published_article, :category => also_used) }
      Factory(:unpublished_article, :category => unpublished)

      assert_equal [used, also_used], Category.used
    end

  end

end
939 Bytes in 3 ms with coderay