Comments (9)
Is Test
mocked? I added a mock block and it started working.
Spectator.describe Test do
mock Test do
stub method2
end
describe "#method1" do
it do
expect(subject).to receive(:method2)
subject.method1
end
end
end
from spectator.
hmmm stubbing one or both methods doesn't seem to be working:
describe "#~" do
mock Chars::CharSet do
stub :===
end
let(other) { "foo" }
it "must call #===" do
expect(subject).to receive(:===).with(other)
subject =~ other
end
end
1) Chars::CharSet#~ must call #===
Failure: subject did not receive #===("foo") : Nil at spec/char_set_spec.cr:770 at least once with any arguments
expected: At least once with any arguments
received: 0 time(s)
from spectator.
Should be resolved in v0.9.40, can you verify?
from spectator.
Confirmed it works on the original Test example.
Although having trouble with my Chars::CharSet
example.
describe "#~" do
mock Chars::CharSet do
stub :===
end
let(other) { "foo" }
it "must call #===" do
expect(subject).to receive(:===).with(other)
subject =~ other
end
end
Produces:
1) Chars::CharSet#~ must call #===
Failure: subject did not receive #===("foo") : Nil at spec/char_set_spec.cr:770 at least once with any arguments
expected: At least once with any arguments
received: 0 time(s)
from spectator.
Changing the stub
syntax to stub ===(other)
causes another odd compilation error:
In spec/char_set_spec.cr:6:9
6 | let(char_range) { 'A'..'Z' }
^---
Error: wrong number of arguments for macro 'stub' (given 0, expected )
from spectator.
There's a combination of things going on here. Because of some syntactical things, ===
must be a symbol on the stub
line. The arguments also have to be defined slightly different. The following works for me:
class Matcher
def =~(other)
self === other
end
def ===(other)
true
end
end
describe Matcher do
mock Matcher do
stub :===, other
end
describe "#~" do
let(other) { "foo" }
it "must call #===" do
expect(subject).to receive(:===).with(other).and_return(true)
subject =~ other
end
end
end
I had to add and_return(true)
to the expect
line because it returns nil if that clause isn't provided.
from spectator.
Hmm getting a weird error:
describe "#~" do
mock Chars::CharSet do
stub :===, other
end
let(other) { "foo" }
let(return_value) { true }
it "must call #===" do
expect(subject).to receive(:===).with(other).and_return(return_value)
expect(subject =~ other).to be(return_value)
end
end
There was a problem expanding macro 'stub'
Code in spec/char_set_spec.cr:6:9
6 | let(char_range) { 'A'..'Z' }
^
Called macro defined in lib/spectator/src/spectator/mocks/stubs.cr:3:13
3 | private macro stub(definition, *types, _file = __FILE__, _line = __LINE__, return_type = :undefined, &block)
Which expanded to:
> 9 | __temp_438.mocks.record_call(self, __temp_440)
> 10 | if (__temp_441 = __temp_438.mocks.find_stub(self, __temp_440))
> 11 | return __temp_441.call!(__temp_439) { previous_def }
^-----------
Error: there is no previous definition of '==='
from spectator.
I'm not sure why that's happening. I've tried (what I think you're working on) with this snippet and it works:
describe "#~" do
mock Chars::CharSet do
stub :===, other : String
end
let(other) { "foo" }
let(return_value) { true }
it "must call #===" do
expect(subject).to receive(:===).with(other).and_return(return_value)
expect(subject =~ other).to be(return_value)
end
end
I added : String
type restriction to match the original definition.
But with the code you posted, it compiles, but fails since the method isn't stubbed right (needs the String
type restriction on other
). Perhaps you can send along the code if this snippet doesn't work with what you have.
Sorry about all of this back and forth. The mock support in Spectator could certainly use some improvements.
https://gist.github.com/icy-arctic-fox/63e500c462107cf29ce1c6fb1d3b652c
from spectator.
Aaaaah the : String
constraint fixed it. Thanks!
from spectator.
Related Issues (20)
- stub method(arg1,arg2,...) syntax seems to no longer work in 0.10/master HOT 3
- How to spec classes that contain class which shadow top-level primitives? HOT 4
- How to mock/stub IO::Memory#gets HOT 8
- Noticed line/column skew during incremental porting of RSpec files over to Spectator HOT 1
- Add RSpec's stub_const
- Better error when attempting to use string interpolation in it descriptions? HOT 11
- Problem with mock when using abstract class HOT 7
- Stubbed methods with default arguments not working HOT 3
- Mock not working for Process.run with block. HOT 10
- Runner filter -e tests equality instead of inclusion
- Example of custom matcher does not compile HOT 1
- Usage of keyword arguments as positional arguments in stubs HOT 1
- Mocks fail to compile for methods with free variables HOT 8
- Attempting to mock File results in error HOT 4
- Allow mocking libs
- Mocks on nameless splats HOT 2
- Re-run only failures HOT 1
- Segfault with nilable substring method call HOT 2
- `be_within` not respected during a `to have_received.with` check HOT 9
- subject with structs HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spectator.