rspec_candy 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. data/.gitignore +8 -0
  2. data/README.md +7 -0
  3. data/Rakefile +18 -0
  4. data/lib/rspec_candy/helpers/disposable_copy.rb +16 -0
  5. data/lib/rspec_candy/helpers/it_should_act_like.rb +39 -0
  6. data/lib/rspec_candy/helpers/new_with_stubs.rb +16 -0
  7. data/lib/rspec_candy/helpers/rails/create_without_callbacks.rb +25 -0
  8. data/lib/rspec_candy/helpers/rails/it_should_run_callbacks.rb +111 -0
  9. data/lib/rspec_candy/helpers/rails/prevent_storage.rb +20 -0
  10. data/lib/rspec_candy/helpers/should_receive_and_execute.rb +38 -0
  11. data/lib/rspec_candy/helpers/should_receive_and_return.rb +17 -0
  12. data/lib/rspec_candy/helpers/should_receive_chain.rb +43 -0
  13. data/lib/rspec_candy/helpers/stub_any_instance.rb +25 -0
  14. data/lib/rspec_candy/helpers/stub_existing.rb +20 -0
  15. data/lib/rspec_candy/switcher.rb +44 -0
  16. data/lib/rspec_candy/version.rb +3 -0
  17. data/lib/rspec_candy.rb +16 -0
  18. data/rspec_candy.gemspec +19 -0
  19. data/spec/rspec1/Gemfile +13 -0
  20. data/spec/rspec1/Rakefile +11 -0
  21. data/spec/rspec1/app_root/config/boot.rb +114 -0
  22. data/spec/rspec1/app_root/config/database.yml +21 -0
  23. data/spec/rspec1/app_root/config/environment.rb +14 -0
  24. data/spec/rspec1/app_root/config/environments/in_memory.rb +0 -0
  25. data/spec/rspec1/app_root/config/environments/mysql.rb +0 -0
  26. data/spec/rspec1/app_root/config/environments/postgresql.rb +0 -0
  27. data/spec/rspec1/app_root/config/environments/sqlite.rb +0 -0
  28. data/spec/rspec1/app_root/config/environments/sqlite3.rb +0 -0
  29. data/spec/rspec1/app_root/config/initializers/state_machine.rb +1 -0
  30. data/spec/rspec1/app_root/config/routes.rb +4 -0
  31. data/spec/rspec1/app_root/log/.gitignore +1 -0
  32. data/spec/rspec1/rcov.opts +2 -0
  33. data/spec/rspec1/spec.opts +4 -0
  34. data/spec/rspec1/spec_helper.rb +24 -0
  35. data/spec/rspec2/.rspec +2 -0
  36. data/spec/rspec2/Gemfile +13 -0
  37. data/spec/rspec2/Rakefile +11 -0
  38. data/spec/rspec2/app_root/.gitignore +4 -0
  39. data/spec/rspec2/app_root/config/application.rb +32 -0
  40. data/spec/rspec2/app_root/config/boot.rb +13 -0
  41. data/spec/rspec2/app_root/config/database.yml +21 -0
  42. data/spec/rspec2/app_root/config/environment.rb +5 -0
  43. data/spec/rspec2/app_root/config/environments/in_memory.rb +0 -0
  44. data/spec/rspec2/app_root/config/environments/mysql.rb +0 -0
  45. data/spec/rspec2/app_root/config/environments/postgresql.rb +0 -0
  46. data/spec/rspec2/app_root/config/environments/sqlite.rb +0 -0
  47. data/spec/rspec2/app_root/config/environments/sqlite3.rb +0 -0
  48. data/spec/rspec2/app_root/config/environments/test.rb +35 -0
  49. data/spec/rspec2/app_root/config/initializers/backtrace_silencers.rb +7 -0
  50. data/spec/rspec2/app_root/config/initializers/inflections.rb +10 -0
  51. data/spec/rspec2/app_root/config/initializers/mime_types.rb +5 -0
  52. data/spec/rspec2/app_root/config/initializers/secret_token.rb +7 -0
  53. data/spec/rspec2/app_root/config/initializers/session_store.rb +8 -0
  54. data/spec/rspec2/app_root/config/initializers/state_machine.rb +1 -0
  55. data/spec/rspec2/app_root/config/locales/en.yml +5 -0
  56. data/spec/rspec2/app_root/config/routes.rb +58 -0
  57. data/spec/rspec2/app_root/log/.gitkeep +0 -0
  58. data/spec/rspec2/rcov.opts +2 -0
  59. data/spec/rspec2/spec_helper.rb +25 -0
  60. data/spec/shared/app_root/app/controllers/application_controller.rb +2 -0
  61. data/spec/shared/app_root/app/models/model.rb +19 -0
  62. data/spec/shared/app_root/app/models/state_machine_model.rb +30 -0
  63. data/spec/shared/app_root/config/initializers/state_machine.rb +1 -0
  64. data/spec/shared/app_root/db/migrate/001_create_model.rb +13 -0
  65. data/spec/shared/app_root/db/migrate/002_create_state_machine_model.rb +13 -0
  66. data/spec/shared/rspec_candy/helpers/disposable_copy_spec.rb +45 -0
  67. data/spec/shared/rspec_candy/helpers/it_should_act_like_spec.rb +139 -0
  68. data/spec/shared/rspec_candy/helpers/new_with_stubs_spec.rb +41 -0
  69. data/spec/shared/rspec_candy/helpers/rails/create_without_callbacks_spec.rb +41 -0
  70. data/spec/shared/rspec_candy/helpers/rails/it_should_run_callbacks_spec.rb +142 -0
  71. data/spec/shared/rspec_candy/helpers/rails/prevent_storage_spec.rb +85 -0
  72. data/spec/shared/rspec_candy/helpers/should_receive_and_execute_spec.rb +32 -0
  73. data/spec/shared/rspec_candy/helpers/should_receive_and_return_spec.rb +30 -0
  74. data/spec/shared/rspec_candy/helpers/should_receive_chain_spec.rb +99 -0
  75. data/spec/shared/rspec_candy/helpers/stub_any_instance_spec.rb +32 -0
  76. data/spec/shared/rspec_candy/helpers/stub_existing_spec.rb +27 -0
  77. data/spec/shared/support/matchers/pass_as_describe_block.rb +32 -0
  78. data/spec/shared/support/matchers/pass_as_example.rb +30 -0
  79. data/template/spec_candy.rails2.rb +171 -0
  80. data/template/spec_candy.rails3.rb +175 -0
  81. metadata +159 -0
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe RSpecCandy::Helpers::Rails::CreateWithoutCallbacks do
4
+
5
+ describe ActiveRecord::Base do
6
+
7
+ describe '.create_without_callbacks' do
8
+
9
+ it 'should create a record without running callbacks or validations' do
10
+ model = Model.disposable_copy do
11
+ before_save :crash
12
+ validate :crash
13
+ def crash
14
+ raise 'callback was run'
15
+ end
16
+ end
17
+ record = nil
18
+ expect do
19
+ record = model.create_without_callbacks(:string_field => 'foo')
20
+ end.to_not raise_error
21
+ record.string_field.should == 'foo'
22
+ record.should be_a(Model)
23
+ record.id.should be_a(Fixnum)
24
+ record.should_not be_new_record
25
+ end
26
+
27
+ end
28
+
29
+ describe '.new_and_store' do
30
+
31
+ it 'should print a deprecation warning urging to use .create_without_callbacks instead' do
32
+ Model.should_receive(:warn).with(/Use create_without_callbacks instead/i)
33
+ Model.should_receive(:create_without_callbacks).with('args')
34
+ Model.new_and_store('args')
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,142 @@
1
+ require 'spec_helper'
2
+
3
+ describe RSpecCandy::Helpers::Rails::ItShouldRunCallbacks do
4
+
5
+ describe '#it_should_run_callbacks' do
6
+
7
+ context 'for an app without state_machine' do
8
+
9
+ it 'should pass if all callbacks have been run' do
10
+ <<-describe_block.should pass_as_describe_block
11
+ describe Model, '#after_save' do
12
+ it_should_run_callbacks :after_save_callback2, :after_save_callback1
13
+ end
14
+ describe_block
15
+ end
16
+
17
+ it 'should only attempt to run the proper callbacks have been run' do
18
+ <<-describe_block.should fail_as_describe_block
19
+ describe Model, '#before_save' do
20
+ it_should_run_callbacks :after_save_callback2, :after_save_callback1
21
+ end
22
+ describe_block
23
+ end
24
+
25
+ it 'should not pass if a callback has not been run' do
26
+ <<-describe_block.should fail_as_describe_block
27
+ describe Model, '#after_create' do
28
+ it_should_run_callbacks :after_create_callback, :unknown_callback
29
+ end
30
+ describe_block
31
+ end
32
+
33
+ it 'should not actually execute the callbacks' do
34
+ <<-describe_block.should pass_as_describe_block
35
+ def Model.after_save_callback2
36
+ raise "called!"
37
+ end
38
+
39
+ describe Model, '#after_save' do
40
+ it_should_run_callbacks :after_save_callback1
41
+ end
42
+ describe_block
43
+ end
44
+
45
+ it 'should not parse state_machine callbacks being present' do
46
+ <<-describe_block.should fail_as_describe_block
47
+ describe StateMachineModel, '#event from :state1 to :state2' do
48
+ it_should_run_callbacks :event_callback
49
+ end
50
+ describe_block
51
+ end
52
+
53
+ end
54
+
55
+ context 'for an app with state_machine' do
56
+
57
+ before do
58
+ ENV['REQUIRE_STATE_MACHINE'] = 'true'
59
+ end
60
+
61
+ after do
62
+ ENV['REQUIRE_STATE_MACHINE'] = 'false'
63
+ end
64
+
65
+ it 'should pass if all callbacks have been run' do
66
+ <<-describe_block.should pass_as_describe_block
67
+ describe Model, '#after_save' do
68
+ it_should_run_callbacks :after_save_callback2, :after_save_callback1
69
+ end
70
+ describe_block
71
+ end
72
+
73
+ it 'should not pass if a callback has not been run' do
74
+ <<-describe_block.should fail_as_describe_block
75
+ describe Model, '#after_create' do
76
+ it_should_run_callbacks :after_create_callback, :unknown_callback
77
+ end
78
+ describe_block
79
+ end
80
+
81
+
82
+ it 'should also run state machine callbacks and pass if they are called if StateMachine is present' do
83
+ <<-describe_block.should pass_as_describe_block
84
+ describe StateMachineModel, '#event from :state1 to :state2' do
85
+ it_should_run_callbacks :event_callback
86
+ end
87
+ describe_block
88
+ end
89
+
90
+ it 'should also run state machine callbacks and fail if they are not called if StateMachine is present' do
91
+ <<-describe_block.should fail_as_describe_block
92
+ describe StateMachineModel, '#event from :state1 to :state3' do
93
+ it_should_run_callbacks :event_callback
94
+ end
95
+ describe_block
96
+ end
97
+
98
+ end
99
+
100
+
101
+ end
102
+
103
+ describe '#it_should_run_callbacks_in_order' do
104
+
105
+ supported_callbacks = case RSpecCandy::Switcher.rspec_version
106
+ when :rspec1
107
+ %w[before after]
108
+ when :rspec2
109
+ %w[before after around]
110
+ end
111
+
112
+ supported_callbacks.each do |kind|
113
+
114
+ it "should pass if all #{kind} callbacks are run in order" do
115
+ <<-describe_block.should pass_as_describe_block
116
+ describe Model, '##{kind}_save' do
117
+ it_should_run_callbacks_in_order :#{kind}_save_callback1, :#{kind}_save_callback2
118
+ end
119
+ describe_block
120
+ end
121
+
122
+ it "should not pass if a #{kind} callback has not been run" do
123
+ <<-describe_block.should fail_as_describe_block
124
+ describe Model, '##{kind}_create' do
125
+ it_should_run_callbacks_in_order :#{kind}_create_callback, :unknown_callback
126
+ end
127
+ describe_block
128
+ end
129
+
130
+ it "should not pass if #{kind} callbacks are run out of order" do
131
+ <<-describe_block.should fail_as_describe_block
132
+ describe Model, '##{kind}_save' do
133
+ it_should_run_callbacks_in_order :#{kind}_save_callback2, :#{kind}_save_callback1
134
+ end
135
+ describe_block
136
+ end
137
+
138
+ end
139
+
140
+ end
141
+
142
+ end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+
3
+ describe RSpecCandy::Helpers::Rails::PreventStorage do
4
+
5
+ describe ActiveRecord::Base do
6
+
7
+ describe '#prevent_storage' do
8
+
9
+ context 'when attempting to save the record afterwards' do
10
+
11
+ it 'should run validations' do
12
+ klass = Model.disposable_copy do
13
+ validate :validation_method
14
+ end
15
+ record = klass.new
16
+ record.prevent_storage
17
+ record.should_receive :validation_method
18
+ record.save
19
+ end
20
+
21
+ it 'should run before_validate callbacks' do
22
+ klass = Model.disposable_copy do
23
+ before_validation :callback_method
24
+ end
25
+ record = klass.new
26
+ record.prevent_storage
27
+ record.should_receive :callback_method
28
+ record.save
29
+ end
30
+
31
+ it 'should run after_validate callbacks' do
32
+ klass = Model.disposable_copy do
33
+ after_validation :callback_method
34
+ end
35
+ record = klass.new
36
+ record.prevent_storage
37
+ record.should_receive :callback_method
38
+ record.save
39
+ end
40
+
41
+ it 'should prevent the record from being committed to the database' do
42
+ record = Model.new
43
+ record.prevent_storage
44
+ record.save.should be_false
45
+ Model.count.should be_zero
46
+ end
47
+
48
+ it 'should not run before_save callbacks' do
49
+ klass = Model.disposable_copy do
50
+ before_save :callback_method
51
+ end
52
+ record = klass.new
53
+ record.prevent_storage
54
+ record.should_not_receive :callback_method
55
+ record.save
56
+ end
57
+
58
+ it 'should not run after_save callbacks' do
59
+ klass = Model.disposable_copy do
60
+ after_save :callback_method
61
+ end
62
+ record = klass.new
63
+ record.prevent_storage
64
+ record.should_not_receive :callback_method
65
+ record.save
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+
72
+ describe '#keep_invalid!' do
73
+
74
+ it 'should print a deprecation warning asking to use #prevent_storage' do
75
+ record = Model.new
76
+ record.should_receive(:warn).with(/Use prevent_storage instead/i)
77
+ record.should_receive(:prevent_storage)
78
+ record.keep_invalid!
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+
85
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe RSpecCandy::Helpers::ShouldReceiveAndExecute do
4
+
5
+ describe Object do
6
+
7
+ describe '#should_receive_and_execute' do
8
+
9
+ it 'should not stub away the original method implementation' do
10
+ object = "object"
11
+ object.should_receive_and_execute(:size)
12
+ object.size.should == 6
13
+ end
14
+
15
+ it 'should set an expectation that the given method are called' do
16
+ (<<-example).should fail_as_example
17
+ object = 'object'
18
+ object.should_receive_and_execute(:size)
19
+ example
20
+ end
21
+
22
+ it 'should return the expectation for further parameterization' do
23
+ object = 'object'
24
+ object.should_receive_and_execute(:size).class.name.should include('MessageExpectation')
25
+ object.size # make the example pass
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe RSpecCandy::Helpers::StubExisting do
4
+
5
+ describe Object do
6
+
7
+ describe '#should_receive_and_return' do
8
+
9
+ it 'should stub out the given methods with the given return values' do
10
+ object = 'object'
11
+ object.should_receive_and_return(:foo => 'foo', :bar => 'bar')
12
+ object.foo.should == 'foo'
13
+ object.bar.should == 'bar'
14
+ object.size.should == 6 # object still responds to unstubbed methods
15
+ end
16
+
17
+ it 'should set expectations that all given methods are called' do
18
+ (<<-example).should fail_as_example
19
+ object = 'object'
20
+ object.should_receive_and_return(:foo => 'foo', :bar => 'bar')
21
+ object.foo
22
+ # but not object.bar
23
+ example
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ describe RSpecCandy::Helpers::ShouldReceiveChain do
4
+
5
+ describe Object do
6
+
7
+ describe '#should_receive_chain' do
8
+
9
+ it "should pass when the chain was traversed completely" do
10
+ (<<-example).should pass_as_example
11
+ object = "object"
12
+ object.should_receive_chain(:first_message, :second_message)
13
+ object.first_message.second_message
14
+ example
15
+ end
16
+
17
+ it "should fail when the chain wasn't traversed at all" do
18
+ (<<-example).should fail_as_example
19
+ object = "object"
20
+ object.should_receive_chain(:first_message, :second_message)
21
+ example
22
+ end
23
+
24
+ it "should fail when the chain was only traversed partially" do
25
+ (<<-example).should fail_as_example
26
+ object = "object"
27
+ object.should_receive_chain(:first_message, :second_message)
28
+ object.first_message
29
+ example
30
+ end
31
+
32
+ it "should allow to add argument expectations to inner chain links by using array notation" do
33
+ (<<-example).should pass_as_example
34
+ object = "object"
35
+ object.should_receive_chain([:first_message, 'argument'], :second_message)
36
+ object.first_message('argument').second_message
37
+ example
38
+ (<<-example).should fail_as_example
39
+ object = "object"
40
+ object.should_receive_chain([:first_message, 'argument'], :second_message)
41
+ object.first_message('wrong argument').second_message
42
+ example
43
+ end
44
+
45
+ it "should allow to add argument expectations to the last chain link by using regular should_receive options" do
46
+ (<<-example).should pass_as_example
47
+ object = "object"
48
+ object.should_receive_chain(:first_message, :second_message).with('argument')
49
+ object.first_message.second_message('argument')
50
+ example
51
+ (<<-example).should fail_as_example
52
+ object = "object"
53
+ object.should_receive_chain(:first_message, :second_message).with('argument')
54
+ object.first_message.second_message('wrong argument')
55
+ example
56
+ end
57
+
58
+ it 'should allow inner chain links to be called more than once' do
59
+ (<<-example).should pass_as_example
60
+ object = "object"
61
+ object.should_receive_chain(:first_message, :second_message)
62
+ object.first_message
63
+ object.first_message.second_message
64
+ example
65
+ end
66
+
67
+ end
68
+
69
+
70
+ describe '#should_not_receive_chain' do
71
+
72
+ it "should fail when the chain was traversed completely" do
73
+ (<<-example).should fail_as_example
74
+ object = "object"
75
+ object.should_not_receive_chain(:first_message, :second_message)
76
+ object.first_message.second_message
77
+ example
78
+ end
79
+
80
+ it "should pass when the chain wasn't traversed at all" do
81
+ (<<-example).should pass_as_example
82
+ object = "object"
83
+ object.should_not_receive_chain(:first_message, :second_message)
84
+ example
85
+ end
86
+
87
+ it "should pass when the chain was only traversed partially" do
88
+ (<<-example).should pass_as_example
89
+ object = "object"
90
+ object.should_not_receive_chain(:first_message, :second_message)
91
+ object.first_message
92
+ example
93
+ end
94
+
95
+ end
96
+
97
+ end
98
+
99
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe RSpecCandy::Helpers::StubAnyInstance do
4
+
5
+ describe Class do
6
+
7
+ describe '#stub_any_instance' do
8
+
9
+ it 'should apply the given stubs to all future instances of that class' do
10
+ klass = Class.new {}
11
+ klass.stub_any_instance(:stubbed_method => 'result')
12
+ 2.times do
13
+ klass.new.stubbed_method.should == 'result'
14
+ end
15
+ end
16
+
17
+ it 'should not prevent initializer code to be run' do
18
+ klass = Class.new do
19
+ attr_reader :initializer_run
20
+ def initialize
21
+ @initializer_run = true
22
+ end
23
+ end
24
+ klass.stub_any_instance(:stubbed_method => 'result')
25
+ klass.new.initializer_run.should == true
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+
32
+ end