Add count modifiers for have_received matcher

This commit is contained in:
Michael Miller 2022-07-14 20:46:52 -06:00
parent e2e33e440b
commit 7e09016e5c
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF
3 changed files with 435 additions and 0 deletions

View file

@ -156,4 +156,184 @@ Spectator.describe "Stubbable receiver DSL" do
expect(fake).to_not have_received(:foo).with(:baz)
end
end
context "count modifiers" do
double(:dbl, foo: 42)
let(dbl) { double(:dbl) }
describe "#once" do
it "matches when the stub is called once" do
dbl.foo
expect(dbl).to have_received(:foo).once
end
it "doesn't match when the stub isn't called" do
expect(dbl).to_not have_received(:foo).once
end
it "doesn't match when the stub is called twice" do
2.times { dbl.foo }
expect(dbl).to_not have_received(:foo).once
end
end
describe "#twice" do
it "matches when the stub is called twice" do
2.times { dbl.foo }
expect(dbl).to have_received(:foo).twice
end
it "doesn't match when the stub isn't called" do
expect(dbl).to_not have_received(:foo).twice
end
it "doesn't match when the stub is called once" do
dbl.foo
expect(dbl).to_not have_received(:foo).twice
end
it "doesn't match when the stub is called thrice" do
3.times { dbl.foo }
expect(dbl).to_not have_received(:foo).twice
end
end
describe "#exactly" do
it "matches when the stub is called the exact amount" do
3.times { dbl.foo }
expect(dbl).to have_received(:foo).exactly(3).times
end
it "doesn't match when the stub isn't called" do
expect(dbl).to_not have_received(:foo).exactly(3).times
end
it "doesn't match when the stub is called less than the amount" do
2.times { dbl.foo }
expect(dbl).to_not have_received(:foo).exactly(3).times
end
it "doesn't match when the stub is called more than the amount" do
4.times { dbl.foo }
expect(dbl).to_not have_received(:foo).exactly(3).times
end
end
describe "#at_least" do
it "matches when the stub is called the exact amount" do
3.times { dbl.foo }
expect(dbl).to have_received(:foo).at_least(3).times
end
it "doesn't match when the stub isn't called" do
expect(dbl).to_not have_received(:foo).at_least(3).times
end
it "doesn't match when the stub is called less than the amount" do
2.times { dbl.foo }
expect(dbl).to_not have_received(:foo).at_least(3).times
end
it "matches when the stub is called more than the amount" do
4.times { dbl.foo }
expect(dbl).to have_received(:foo).at_least(3).times
end
end
describe "#at_most" do
it "matches when the stub is called the exact amount" do
3.times { dbl.foo }
expect(dbl).to have_received(:foo).at_most(3).times
end
it "matches when the stub isn't called" do
expect(dbl).to have_received(:foo).at_most(3).times
end
it "matches when the stub is called less than the amount" do
2.times { dbl.foo }
expect(dbl).to have_received(:foo).at_most(3).times
end
it "doesn't match when the stub is called more than the amount" do
4.times { dbl.foo }
expect(dbl).to_not have_received(:foo).at_most(3).times
end
end
describe "#at_least_once" do
it "matches when the stub is called once" do
dbl.foo
expect(dbl).to have_received(:foo).at_least_once
end
it "doesn't match when the stub isn't called" do
expect(dbl).to_not have_received(:foo).at_least_once
end
it "matches when the stub is called more than once" do
2.times { dbl.foo }
expect(dbl).to have_received(:foo).at_least_once
end
end
describe "#at_least_twice" do
it "doesn't match when the stub is called once" do
dbl.foo
expect(dbl).to_not have_received(:foo).at_least_twice
end
it "doesn't match when the stub isn't called" do
expect(dbl).to_not have_received(:foo).at_least_twice
end
it "matches when the stub is called twice" do
2.times { dbl.foo }
expect(dbl).to have_received(:foo).at_least_twice
end
it "matches when the stub is called more than twice" do
3.times { dbl.foo }
expect(dbl).to have_received(:foo).at_least_twice
end
end
describe "#at_most_once" do
it "matches when the stub is called once" do
dbl.foo
expect(dbl).to have_received(:foo).at_most_once
end
it "matches when the stub isn't called" do
expect(dbl).to have_received(:foo).at_most_once
end
it "doesn't match when the stub is called more than once" do
2.times { dbl.foo }
expect(dbl).to_not have_received(:foo).at_most_once
end
end
describe "#at_most_twice" do
it "matches when the stub is called once" do
dbl.foo
expect(dbl).to have_received(:foo).at_most_twice
end
it "matches when the stub isn't called" do
expect(dbl).to have_received(:foo).at_most_twice
end
it "matches when the stub is called twice" do
2.times { dbl.foo }
expect(dbl).to have_received(:foo).at_most_twice
end
it "doesn't match when the stub is called more than twice" do
3.times { dbl.foo }
expect(dbl).to_not have_received(:foo).at_most_twice
end
end
end
end