Skip to content

Commit 3857fe1

Browse files
committed
Add example of usage shared_examples in RSpec tests.
1 parent bce4ec7 commit 3857fe1

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,7 @@ they will retry the match for given timeout allowing you to test ajax actions.
10841084
end
10851085
```
10861086

1087+
10871088
* Use `its` when possible
10881089

10891090
```Ruby
@@ -1103,6 +1104,44 @@ they will retry the match for given timeout allowing you to test ajax actions.
11031104
end
11041105
```
11051106

1107+
1108+
* Use `shared_examples` if you want to create a spec group that can be shared by many other tests.
1109+
1110+
```Ruby
1111+
# bad
1112+
describe Array do
1113+
subject { Array.new [7, 2, 4] }
1114+
1115+
context "initialized with 3 items" do
1116+
its(:size) { should eq(3) }
1117+
end
1118+
end
1119+
1120+
describe Set do
1121+
subject { Set.new [7, 2, 4] }
1122+
1123+
context "initialized with 3 items" do
1124+
its(:size) { should eq(3) }
1125+
end
1126+
end
1127+
1128+
#good
1129+
shared_examples "a collection" do
1130+
subject { described_class.new([7, 2, 4]) }
1131+
1132+
context "initialized with 3 items" do
1133+
its(:size) { should eq(3) }
1134+
end
1135+
end
1136+
1137+
describe Array do
1138+
it_behaves_like "a collection"
1139+
end
1140+
1141+
describe Set do
1142+
it_behaves_like "a collection"
1143+
end
1144+
11061145
### Views
11071146
11081147
* The directory structure of the view specs `spec/views` matches the

0 commit comments

Comments
 (0)