cirandas.net

ref: master

test/functional/kinds_controller_test.rb


 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require_relative '../test_helper'

class KindsControllerTest < ActionController::TestCase
  def setup
    @controller = KindsController.new
    @environment = Environment.default
    create_user_with_permission('adminuser', 'manage_environment_kinds', environment)
    login_as('adminuser')
  end

  attr_accessor :environment

  should 'list kinds' do
    pk1 = Kind.create(:environment_id => environment.id, :type => 'Person', :name => 'Captain')
    pk2 = Kind.create(:environment_id => environment.id, :type => 'Person', :name => 'Officer')

    ck1 = Kind.create(:environment_id => environment.id, :type => 'Community', :name => 'Discussion')
    ck2 = Kind.create(:environment_id => environment.id, :type => 'Community', :name => 'Decision')
    ck3 = Kind.create(:environment_id => environment.id, :type => 'Community', :name => 'Proposal')

    ek1 = Kind.create(:environment_id => environment.id, :type => 'Enterprise', :name => 'Market')
    ek2 = Kind.create(:environment_id => environment.id, :type => 'Enterprise', :name => 'Production')

    get 'index'

    assert_equivalent [pk1, pk2], assigns(:kinds)['Person']
    assert_equivalent [ck1, ck2, ck3], assigns(:kinds)['Community']
    assert_equivalent [ek1, ek2], assigns(:kinds)['Enterprise']
  end

  should 'set type on new' do
    get 'new', :kind => {:type => 'Profile'}
    assert_equal 'Profile', assigns(:kind).type
  end

  should 'create new kind' do
    type = 'Person'
    name = 'Captain'

    post 'new', :kind => {:type => type, :name => name}

    kind = environment.kinds.where(:name => name).first
    assert kind.present?
    assert_equal name, kind.name
    assert_equal type, kind.type
  end

  should 'edit kind' do
    name = 'Officer'
    type = 'Person'
    kind = Kind.create(:environment_id => environment.id, :type => 'Person', :name => 'Captain')

    post 'edit', :id => kind.id, :kind => {:type => type, :name => name}

    kind.reload
    assert_equal name, kind.name
    assert_equal type, kind.type
  end

  should 'destroy kind' do
    kind = Kind.create(:environment_id => environment.id, :type => 'Person', :name => 'Captain')
    post 'destroy', :id => kind.id
    assert_raise ActiveRecord::RecordNotFound do
      kind.reload
    end
  end
end