-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathREADME.md.erb
1480 lines (1106 loc) · 43 KB
/
README.md.erb
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
[](http://badge.fury.io/rb/transpec)
[](https://travis-ci.org/yujinakayama/transpec)
[](https://coveralls.io/github/yujinakayama/transpec?branch=master)
[](https://codeclimate.com/github/yujinakayama/transpec)
# Transpec
**Transpec** is a tool for converting your specs to the latest [RSpec](https://relishapp.com/rspec/) syntax with static and dynamic code analysis.
With Transpec you can upgrade your RSpec 2 specs to RSpec 3 in no time.
It supports [conversions](#supported-conversions) for almost all of the RSpec 3 changes – not only the `expect` syntax.
Also, you can use it on your RSpec 2 project even if you're not going to upgrade it to RSpec 3 for now.
Check out the following posts for the new RSpec syntax and the changes in RSpec 3:
* [RSpec's New Expectation Syntax](http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/)
* [RSpec's new message expectation syntax](http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/)
* [Notable Changes in RSpec 3](http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/)
If you are going to use Transpec in the upgrade process to RSpec 3,
read the RSpec official guide:
* https://relishapp.com/rspec/docs/upgrade
## Examples
Here's an example spec:
```ruby
<%=
example = <<END
describe Account do
subject(:account) { Account.new(logger) }
let(:logger) { mock('logger') }
describe '#balance' do
context 'initially' do
it 'is zero' do
account.balance.should == 0
end
end
end
describe '#close' do
it 'logs an account closed message' do
logger.should_receive(:account_closed).with(account)
account.close
end
end
describe '#renew' do
context 'when the account is not closed' do
before do
account.stub(:closed?).and_return(false)
end
it 'does not raise error' do
lambda { account.renew }.should_not raise_error(Account::RenewalError)
end
end
end
end
END
-%>
```
Transpec would convert it to the following form:
```ruby
<%= convert(example) -%>
```
### Actual examples
You can see actual conversion examples below:
* https://github.com/yujinakayama/guard/commit/transpec-demo
* https://github.com/yujinakayama/mail/commit/transpec-demo
* https://github.com/yujinakayama/twitter/commit/transpec-demo
## Installation
Simply install `transpec` with `gem` command:
```bash
$ gem install transpec
```
Normally you don't need to add `transpec` to your `Gemfile` or `*.gemspec` since this isn't a tool to be used daily.
## Basic Usage
Before converting your specs:
* Make sure your project has `rspec` gem dependency **<%= Transpec.required_rspec_version %>** or later. If not, change your `Gemfile` or `*.gemspec` to do so.
* Run `rspec` and check if all the specs pass.
* Ensure the Git repository is clean. (You don't want to mix up your changes and Transpec's changes, do you?)
Then, run `transpec` in the project root directory:
```bash
$ cd some-project
$ transpec
```
This will run the specs, convert them, and overwrite all spec files in the `spec` directory.
After the conversion, run `rspec` again and check whether everything is still green:
```bash
$ bundle exec rspec
```
If it's green, commit the changes with an auto-generated message
that describes the conversion summary and helps your team members to understand the new syntax:
```bash
$ git commit -aeF .git/COMMIT_EDITMSG
```
And you are done!
## Advanced Usage
### Convert only specific files
You can pass `transpec` arbitrary paths to convert:
```bash
# You always need to be in the project root directory
$ cd some-project
# Convert only files in `features` directory
$ transpec features
# Convert only files in `spec/foo` and `spec/bar` directory
$ transpec spec/foo spec/bar
# Convert only `spec/baz_spec.rb`
$ transpec spec/baz_spec.rb
```
Note that the current working directory always needs to be the project root directory,
so that Transpec can know where the root is.
### Enable/disable specific conversions
You can disable specific conversions that are enabled by default with `-k/--keep` option,
and enable conversions that are disabled by default with the `-v/--convert` option.
If you are willing to try the latest and modern syntax, run the following on RSpec 3:
```
$ transpec --convert example_group,hook_scope
```
See [`-k/--keep`](#-k--keep) and [`-v/--convert`](#-v--convert) for more details.
## Options
Though Transpec ships with sensible defaults that essentially conform to the RSpec 3 defaults,
you can customize the conversion behavior.
### `-f/--force`
Force processing even if the current Git repository is not clean.
```bash
$ git status --short
M spec/spec_helper.rb
$ transpec
The current Git repository is not clean. Aborting. If you want to proceed forcibly, use -f/--force option.
$ transpec --force
Copying project for dynamic analysis...
Running dynamic analysis with command "bundle exec rspec"...
```
### `-c/--rspec-command`
Specify a command to run your specs which is used for dynamic analysis.
Transpec needs to run your specs in a copied project directory for dynamic analysis.
If your project requires some special setup or commands to run specs, use this option.
`bundle exec rspec` is used by default.
Note that the command to run dynamic analysis does _not_ affect to the files or specs to be converted.
This means that even if you specify a command that only runs a subset of the files in your spec suite or a subset of the specs in a file, every spec will be converted.
For this reason, it's recommended to provide a command that runs full spec suite to `-c/--rspec-command`.
If you want to convert only a subset of the files in a spec suite, pass the paths to `transpec`.
See [Advanced Usage](#advanced-usage) for more details.
You can change the temporary directory that the your project will be copied
by specifying the `TMPDIR` environment variable.
```bash
$ transpec --rspec-command "./special_setup.sh && bundle exec rspec"
```
### `-k/--keep`
Keep specific syntaxes by disabling conversions.
```bash
$ transpec --keep should_receive,stub
```
#### Conversions enabled by default
Note that some syntaxes are available only if your project's RSpec is specific version or later.
If they are unavailable, conversions for such syntaxes will be disabled automatically.
Type | Target Syntax | Converted Syntax
-----------------|--------------------------------|-------------------------------------------
<%=
conversion_type_table = <<END
`should` | `obj.should matcher` | `expect(obj).to matcher`
`oneliner` | `it { should ... }` | `it { is_expected.to ... }`
`should_receive` | `obj.should_receive(:message)` | `expect(obj).to receive(:message)`
`stub` | `obj.stub(:message)` | `allow(obj).to receive(:message)`
`have_items` | `expect(obj).to have(n).items` | `expect(obj.size).to eq(n)`
`its` | `its(:attr) { }` | `describe '#attr' { subject { }; it { } }`
`pending` | `pending 'is an example' { }` | `skip 'is an example' { }`
`deprecated` | All other deprecated syntaxes | Latest syntaxes
END
validate_syntax_type_table(conversion_type_table, true)
conversion_type_table
-%>
See [Supported Conversions](#supported-conversions) for more details.
### `-v/--convert`
Enable specific conversions that are disabled by default.
```bash
$ transpec --convert example_group
```
#### Conversions disabled by default
Most of these target syntaxes are _not_ deprecated in both RSpec 2 and 3,
but the new syntaxes provide more modern and clear ways.
Type | Target Syntax | Converted Syntax
-----------------|--------------------------------|----------------------------------------------------
<%=
conversion_type_table = <<END
`example_group` | `describe 'something' { }` | `RSpec.describe 'something' { }`
`hook_scope` | `before(:all) { }` | `before(:context) { }`
`stub_with_hash` | `obj.stub(:message => value)` | `allow(obj).to receive(:message).and_return(value)`
END
validate_syntax_type_table(conversion_type_table, false)
conversion_type_table
-%>
Note: Specifying `stub_with_hash` enables conversion of `obj.stub(:message => value)`
to `allow(obj).to receive(:message).and_return(value)`
when `allow(obj).to receive_messages(:message => value)` is unavailable (prior to RSpec 3.0),
and it will be converted to multiple statements if the hash includes multiple pairs.
If your project's RSpec is 3.0 or later, it will be converted to `receive_messages(:message => value)`
regardless of this option.
See [Supported Conversions - Method stubs with a hash argument](#method-stubs-with-a-hash-argument) for more details.
### `-o/--convert-only`
Convert specific syntaxes while keeping all other syntaxes.
This option would be useful when you want to convert a non-deprecated syntax
while keeping another syntax that would be converted by default.
(e.g. converting the hook scope aliases while keeping the one-liner `should`).
```bash
$ transpec --convert-only example_group,hook_scope
```
### `-s/--skip-dynamic-analysis`
Skip dynamic analysis and convert with only static analysis.
The use of this option is basically **discouraged**
since it significantly decreases the overall conversion accuracy.
This would be useful only if your spec suite takes really long (like an hour) to run
and you prefer a combination of the rough but fast conversion by Transpec and manual fixes after that.
### `-n/--negative-form`
Specify a negative form of `to` which is used in the `expect` syntax.
Either `not_to` or `to_not`.
`not_to` is used by default.
```bash
$ transpec --negative-form to_not
```
### `-b/--boolean-matcher`
Specify a boolean matcher type which `be_true` and `be_false` will be converted to.
Any of `truthy,falsey`, `truthy,falsy` or `true,false` can be specified.
`truthy,falsey` is used by default.
```bash
$ transpec --boolean-matcher true,false
```
See [Supported Conversions - Boolean matchers](#boolean-matchers) for more details.
### `-e/--explicit-spec-type`
Add explicit spec `:type` metadata to example groups in a project using rspec-rails.
See [Supported Conversions - Implicit spec types in rspec-rails](#implicit-spec-types-in-rspec-rails) for more details.
### `-a/--no-yield-any-instance`
Suppress yielding receiver instances to `any_instance` implementation blocks as the first block argument.
By default in RSpec 3, `any_instance` implementation blocks will be yielded the receiving
instance as the first block argument, and by default Transpec converts specs by adding instance arguments to the blocks so that they conform to the behavior of RSpec 3.
Specifying this option suppresses the conversion and keeps them compatible with RSpec 2.
Note that this is not same as `--keep deprecated` since this configures `yield_receiver_to_any_instance_implementation_blocks` with `RSpec.configure`.
See [Supported Conversions - `any_instance` implementation blocks](#any_instance-implementation-blocks) for more details.
### `-p/--no-parens-matcher-arg`
Suppress parenthesizing arguments of matchers when converting
`should` with operator matcher to `expect` with non-operator matcher
(the `expect` syntax does not directly support the operator matchers).
Note that it will be parenthesized even if this option is specified
when parentheses are necessary to keep the meaning of the expression.
```ruby
<%=
example = <<END
describe 'original spec' do
it 'is an example' do
1.should == 1
2.should > 1
'string'.should =~ /^str/
[1, 2, 3].should =~ [2, 1, 3]
{ key: value }.should == { key: value }
end
end
END
-%>
<%= convert(example).gsub('original spec', 'converted spec') -%>
<%=
converted = convert(example, cli: ['-p'])
converted.gsub!(
'original spec',
'converted spec with -p/--no-parens-matcher-arg option'
)
insert_comment_above(converted, '{ key: value }', [
'With non-operator method, the parentheses are always required',
'to prevent the hash from being interpreted as a block.'
])
-%>
```
## Inconvertible Specs
You might see the following warning while conversion:
```
Cannot convert #should into #expect since #expect is not available in the context.
spec/awesome_spec.rb:4: 1.should == 1
```
This message would be shown with specs like:
```ruby
describe '#should that cannot be converted to #expect' do
class MyAwesomeTestRunner
def run
1.should == 1
end
end
it 'is 1' do
test_runner = MyAwesomeTestRunner.new
test_runner.run
end
end
```
### Reason
* `should` is defined on `BasicObject` class, so you can use `should` everywhere.
* `expect` is defined on `RSpec::Matchers` module which is included by `RSpec::Core::ExampleGroup` class, so you can use `expect` only where `self` is an instance of `RSpec::Core::ExampleGroup` (i.e. in `it` blocks, `:each` hook blocks or included module methods) or other classes that explicitly include `RSpec::Matchers`.
With the above example, in the context of `1.should == 1`, the `self` is an instance of `MyAwesomeTestRunner`.
Transpec tracks contexts and skips conversion if the syntax cannot be converted in a case like this.
### Solution
Include or extend any of the following module to make RSpec syntax available in the context:
* `RSpec::Matchers` for `expect(obj).to some_matcher`
* `RSpec::Mocks::ExampleMethods` for `expect/allow(obj).to receive(:message)`
```ruby
class MyAwesomeTestRunner
include RSpec::Matchers
def run
1.should == 1
end
end
```
Then run `transpec` again.
## Two Types of `should`
There are two types of `should`:
```ruby
describe 'the monkey-patched should' do
subject { [] }
it 'is empty' do
subject.should be_empty
# ^^^^^^ BasicObject#should in RSpec 2.11 or later,
# or Kernel#should prior to RSpec 2.11.
end
end
describe 'the one-liner should' do
subject { [] }
it { should be_empty }
# ^^^^^^ RSpec::Core::ExampleGroup#should
end
```
The monkey-patched `obj.should`:
* Is defined on `BasicObject` (or `Kernel`) and provided by `rspec-expectations` gem.
* Is deprecated in RSpec 3.
* Has [the issue](http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/#delegation-issues) with delegate/proxy objects.
* There's the alternative syntax [`expect(obj).to`](#standard-expectations) since RSpec 2.11.
The one-liner (implicit receiver) `should`:
* Is defined on `RSpec::Core::ExampleGroup` and provided by `rspec-core` gem.
* Is _not_ deprecated in RSpec 3.
* Does _not_ have the issue with delegate/proxy objects.
* There's the alternative syntax [`is_expected.to`](#one-liner-expectations) since RSpec 2.99.beta2.
## Supported Conversions
<%=
supported_conversions_section = find_section('Supported Conversions', level: 2)
table_of_contents(supported_conversions_section, level: 3)
%>
### Standard expectations
Targets:
```ruby
<%=
positive_example = <<END
obj.should matcher
END
-%>
<%=
negative_example = <<END
obj.should_not matcher
END
-%>
```
Will be converted to:
```ruby
<%= convert(positive_example, wrap_with: :example) -%>
<%= convert(negative_example, wrap_with: :example) -%>
<%= convert(negative_example, wrap_with: :example, cli: ['--negative-form', 'to_not']).chomp -%> # with `--negative-form to_not`
```
* This conversion can be disabled by: `--keep should`
* Deprecation: deprecated since RSpec 3.0
* See also: [RSpec's New Expectation Syntax](http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/)
### One-liner expectations
This conversion is available only if your project's RSpec is **<%= rspec_version = RSpecVersion.oneliner_is_expected_available_version %> or later**.
Targets:
```ruby
<%=
positive_example = <<END
it { should matcher }
END
-%>
<%=
negative_example = <<END
it { should_not matcher }
END
-%>
```
Will be converted to:
```ruby
<%= convert(positive_example, wrap_with: :example, rspec_version: rspec_version) -%>
<%= convert(negative_example, wrap_with: :example, rspec_version: rspec_version) -%>
<%= convert(negative_example, wrap_with: :example, rspec_version: rspec_version, cli: ['--negative-form', 'to_not']).chomp -%> # with `--negative-form to_not`
```
`is_expected.to` is designed for the consistency with the `expect` syntax.
However the one-liner `should` is still _not_ deprecated in RSpec 3.0
and available even if the `should` syntax is
[disabled with `RSpec.configure`](https://www.relishapp.com/rspec/rspec-expectations/v/3-0/docs/syntax-configuration#disable-should-syntax).
So if you think `is_expected.to` is verbose,
feel free to disable this conversion and continue using the one-liner `should`.
See [Two Types of `should`](#two-types-of-should) also.
* This conversion can be disabled by: `--keep oneliner`
* Deprecation: not deprecated
* See also: [Add `is_expected` for expect-based one-liner syntax. by myronmarston · rspec/rspec-core](https://github.com/rspec/rspec-core/pull/1180)
### Operator matchers
Targets:
```ruby
<%=
example = <<END
1.should == 1
1.should < 2
Integer.should === 1
'string'.should =~ /^str/
[1, 2, 3].should =~ [2, 1, 3]
END
-%>
```
Will be converted to:
```ruby
<%= convert(example, wrap_with: :example) -%>
```
This conversion is combined with the conversion of [standard expectations](#standard-expecatations) and cannot be disabled separately because the `expect` syntax does not directly support the operator matchers.
* See also: [(Almost) All Matchers Are Supported - RSpec's New Expectation Syntax](http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/#almost-all-matchers-are-supported)
### Boolean matchers
This conversion is available only if your project's RSpec is **<%= rspec_version = RSpecVersion.be_truthy_available_version %> or later**.
Targets:
```ruby
<%=
example = <<END
expect(obj).to be_true
expect(obj).to be_false
END
-%>
```
Will be converted to:
```ruby
<%= convert(example, rspec_version: rspec_version, wrap_with: :example) -%>
# With `--boolean-matcher truthy,falsy`
# be_falsy is just an alias of be_falsey.
<%= convert(example, cli: ['--boolean-matcher', 'truthy,falsy'], rspec_version: rspec_version, wrap_with: :example) -%>
# With `--boolean-matcher true,false`
<%= convert(example, cli: ['--boolean-matcher', 'true,false'], rspec_version: rspec_version, wrap_with: :example) -%>
```
* `be_true` matcher passes if expectation subject is _truthy_ in conditional semantics. (i.e. all objects except `false` and `nil`)
* `be_false` matcher passes if expectation subject is _falsey_ in conditional semantics. (i.e. `false` or `nil`)
* `be_truthy` and `be_falsey` matchers are renamed version of `be_true` and `be_false` and their behaviors are same.
* `be true` and `be false` are not new things. These are combinations of `be` matcher and boolean literals. These pass if expectation subject is exactly equal to boolean value.
So, converting `be_true`/`be_false` to `be_truthy`/`be_falsey` never breaks your specs and this is Transpec's default. If you are willing to test boolean values strictly, you can convert them to `be true`/`be false` with `--boolean-matcher true,false` option. Note that this may break your specs if your application code don't return exact boolean values.
* This conversion can be disabled by: `--keep deprecated`
* Deprecation: deprecated since RSpec 2.99, removed in RSpec 3.0
* See also: [Consider renaming `be_true` and `be_false` to `be_truthy` and `be_falsey` · rspec/rspec-expectations](https://github.com/rspec/rspec-expectations/issues/283)
### `be_close` matcher
Targets:
```ruby
<%=
example = <<END
expect(1.0 / 3.0).to be_close(0.333, 0.001)
END
-%>
```
Will be converted to:
```ruby
<%= convert(example, wrap_with: :example) -%>
```
* This conversion can be disabled by: `--keep deprecated`
* Deprecation: deprecated since RSpec 2.1, removed in RSpec 3.0
* See also: [New be within matcher and RSpec.deprecate fix · rspec/rspec-expectations](https://github.com/rspec/rspec-expectations/pull/32)
### `have(n).items` matcher
This conversion will be **disabled automatically if `rspec-collection_matchers` is loaded** in your spec.
Targets:
```ruby
<%=
example = <<END
expect(collection).to have(3).items
expect(collection).to have_exactly(3).items
expect(collection).to have_at_least(3).items
expect(collection).to have_at_most(3).items
END
-%>
<%=
should_example = <<END
collection.should have(3).items
END
-%>
# Assume `team` responds to #players.
<%=
collection_owner_example = <<END
expect(team).to have(3).players
END
-%>
# Assume #players is a private method.
<%=
private_method_example = <<END
expect(team).to have(3).players
END
-%>
# Validation expectations in rspec-rails.
<%=
errors_on_example = <<END
expect(model).to have(2).errors_on(:name)
END
-%>
```
Will be converted to:
```ruby
<%=
convert(example, dynamic: true, wrap_with: :example, hidden: 'collection = [1, 2, 3]')
-%>
<%=
" # With `--keep should`\n" +
convert(should_example, cli: ['--keep', 'should'], dynamic: true, wrap_with: :example, hidden: 'collection = [1, 2, 3]')
-%>
<%=
convert(collection_owner_example, dynamic: true, wrap_with: :example, hidden: <<END)
class Team
def players
[1, 2, 3]
end
end
team = Team.new
END
-%>
# have(n).items matcher invokes #players even if it's a private method.
<%=
convert(private_method_example, dynamic: true, wrap_with: :example, hidden: <<END)
class Team
private
def players
[1, 2, 3]
end
end
team = Team.new
END
-%>
# Conversion of `have(n).errors_on(:attr)` is not supported.
<%=
convert(errors_on_example, dynamic: true, wrap_with: :example, hidden: <<END)
module ActiveModel
module Validations
def errors_on(attribute, options = {})
valid_args = [options[:context]].compact
self.valid?(*valid_args)
[self.errors[attribute]].flatten.compact
end
end
end
class Model
include ActiveModel::Validations
def valid?(*)
false
end
def errors
{ name: [:foo, :bar] }
end
end
model = Model.new
END
-%>
```
There's an option to continue using `have(n).items` matcher with [rspec-collection_matchers](https://github.com/rspec/rspec-collection_matchers) which is a gem extracted from `rspec-expectations`.
If you choose to do so, disable this conversion by either:
* Specify `--keep have_items` option manually.
* Require `rspec-collection_matchers` in your spec so that Transpec automatically disables this conversion.
#### Note about `expect(model).to have(n).errors_on(:attr)`
The idiom `expect(model).to have(n).errors_on(:attr)` in rspec-rails 2 consists of
`have(n).items` matcher and a monkey-patch [`ActiveModel::Validations#errors_on`](https://github.com/rspec/rspec-rails/blob/v2.14.2/lib/rspec/rails/extensions/active_record/base.rb#L34-L57).
In RSpec 2 the monkey-patch was provided by rspec-rails,
but in RSpec 3 it's extracted to rspec-collection_matchers along with `have(n).items` matcher.
So if you convert it to `expect(model.errors_on(:attr).size).to eq(2)` without rspec-collection_matchers,
it fails with error `undefined method 'error_on' for #<Model ...>`.
Technically it can be converted to:
```ruby
model.valid?
expect(model.errors[:attr].size).to eq(n)
```
However currently Transpec doesn't support this conversion
since this is probably not what most people want.
So using rspec-collection_matchers gem is recommended for now.
* This conversion can be disabled by: `--keep have_items`
* Deprecation: deprecated since RSpec 2.99, removed in RSpec 3.0
* See also: [Expectations: `have(x).items` matchers will be moved into an external gem - The Plan for RSpec 3](http://rspec.info/blog/2013/07/the-plan-for-rspec-3/#expectations-havexitems-matchers-will-be-moved-into-an-external-gem)
### One-liner expectations with `have(n).items` matcher
This conversion will be **disabled automatically if `rspec-collection_matchers` is loaded** in your spec.
Targets:
```ruby
<%=
example = <<END
it { should have(3).items }
END
-%>
<%=
collection_owner_example = <<END
it { should have_at_least(3).players }
END
-%>
```
Will be converted to:
```ruby
<%= convert(example, dynamic: true, wrap_with: :group, hidden: 'subject { [1, 2, 3] }') -%>
# With `--keep should`
<%= convert(example, dynamic: true, wrap_with: :group, hidden: 'subject { [1, 2, 3] }', cli: ['--keep', 'should']) -%>
<%=
convert(collection_owner_example, dynamic: true, wrap_with: :group, hidden: <<END)
subject do
class Team
def players
[1, 2, 3]
end
end
Team.new
end
END
-%>
```
* This conversion can be disabled by: `--keep have_items`
### Expectations on block
Targets:
```ruby
<%=
example = <<END
lambda { do_something }.should raise_error
proc { do_something }.should raise_error
-> { do_something }.should raise_error
expect { do_something }.should raise_error
END
-%>
```
Will be converted to:
```ruby
<%= convert(example, wrap_with: :example).lines.to_a.uniq.join("\n") -%>
```
* This conversion can be disabled by: `--keep should`
* Deprecation: deprecated since RSpec 3.0
* See also: [Unification of Block vs. Value Syntaxes - RSpec's New Expectation Syntax](http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/#unification-of-block-vs-value-syntaxes)
### Expectations on attribute of subject with `its`
This conversion will be **disabled automatically if `rspec-its` is loaded** in your spec.
Targets:
```ruby
<%=
example = <<END
describe 'example' do
subject { { foo: 1, bar: 2 } }
its(:size) { should == 2 }
its([:foo]) { should == 1 }
its('keys.first') { should == :foo }
end
END
-%>
```
Will be converted to:
```ruby
<%= convert(example) -%>
```
There's an option to continue using `its` with [rspec-its](https://github.com/rspec/rspec-its) which is a gem extracted from `rspec-core`.
If you choose to do so, disable this conversion by either:
* Specify `--keep its` option manually.
* Require `rspec-its` in your spec so that Transpec automatically disables this conversion.
Note that this conversion is a sort of first-aid
and ideally the expectations should be rewritten to be more expressive by yourself.
Read [this post](https://gist.github.com/myronmarston/4503509) for the rationale.
* This conversion can be disabled by: `--keep its`
* Deprecation: deprecated since RSpec 2.99, removed in RSpec 3.0
* See also: [Core: `its` will be moved into an external gem - The Plan for RSpec 3](http://rspec.info/blog/2013/07/the-plan-for-rspec-3/#core-its-will-be-moved-into-an-external-gem)
### Negative error expectations with specific error
Targets:
```ruby
<%=
example = <<END
expect { do_something }.not_to raise_error(SomeErrorClass)
expect { do_something }.not_to raise_error('message')
expect { do_something }.not_to raise_error(SomeErrorClass, 'message')
END
-%>
<%=
should_example = <<END
lambda { do_something }.should_not raise_error(SomeErrorClass)
END
-%>
```
Will be converted to:
```ruby
<%= convert(example, wrap_with: :example).lines.to_a.uniq.join("\n") -%>
<%= convert(should_example, wrap_with: :example, cli: ['--keep', 'should']).chomp -%> # with `--keep should`
```
* This conversion can be disabled by: `--keep deprecated`
* Deprecation: deprecated since RSpec 2.14, removed in RSpec 3.0
* See also: [Consider deprecating `expect { }.not_to raise_error(SpecificErrorClass)` · rspec/rspec-expectations](https://github.com/rspec/rspec-expectations/issues/231)
### Message expectations
Targets:
```ruby
<%=
example = <<END
obj.should_receive(:message)
Klass.any_instance.should_receive(:message)
END
-%>
```
Will be converted to:
```ruby
<%= convert(example, wrap_with: :example) -%>
```
* This conversion can be disabled by: `--keep should_receive`
* Deprecation: deprecated since RSpec 3.0
* See also: [RSpec's new message expectation syntax](http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/)
### Message expectations that are actually method stubs
Targets:
```ruby
<%=
example = <<END
obj.should_receive(:message).any_number_of_times
obj.should_receive(:message).at_least(0)
END
-%>
<%=
any_instance_example = <<END
Klass.any_instance.should_receive(:message).any_number_of_times
Klass.any_instance.should_receive(:message).at_least(0)
END
-%>
```
Will be converted to:
```ruby
<%= convert(example, wrap_with: :example).lines.to_a.uniq.join("\n") -%>
<%= convert(example, wrap_with: :example, cli: ['--keep', 'stub']).lines.to_a.uniq.join("\n").chomp -%> # with `--keep stub`
<%= convert(any_instance_example, wrap_with: :example).lines.to_a.uniq.join("\n") -%>
<%= convert(any_instance_example, wrap_with: :example, cli: ['--keep', 'stub']).lines.to_a.uniq.join("\n").chomp -%> # with `--keep stub`
```
* This conversion can be disabled by: `--keep deprecated`
* Deprecation: deprecated since RSpec 2.14, removed in RSpec 3.0
* See also: [Don't allow at_least(0) · rspec/rspec-mocks](https://github.com/rspec/rspec-mocks/issues/133)
### Method stubs
Targets:
```ruby
<%=
example = <<END
obj.stub(:message)
obj.stub!(:message)
END
-%>
<%=
stub_chain_example = <<END
obj.stub_chain(:foo, :bar, :baz)
END
-%>
<%=
any_instance_example = <<END
Klass.any_instance.stub(:message)
END
-%>
<%=
unstub_example = <<END
obj.unstub(:message)
obj.unstub!(:message)
END
-%>
```
Will be converted to:
```ruby
<%= convert(example, wrap_with: :example).lines.to_a.uniq.join("\n") -%>
# Conversion from `stub_chain` to `receive_message_chain` is available
# only if the target project's RSpec is <%= rspec_version = RSpecVersion.receive_message_chain_available_version %> or later
<%= convert(stub_chain_example, wrap_with: :example, rspec_version: rspec_version) -%>
<%= convert(any_instance_example, wrap_with: :example) -%>
<%= convert(unstub_example, wrap_with: :example).lines.to_a.uniq.join("\n") -%>