New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support symbols as stub names #14
Comments
This is kind of a new feature, but I would expect it to work with symbols anyways. What's problematic here is that there's no syntactically correct way to represent a stub for an operator. What I have done is add support for this syntax: struct Node
def text
"foo"
end
def [](index)
42
end
def []?(index)
42.as(Int32?)
end
end
Spectator.describe Node do
mock Node do
stub :text { "bar" }
stub :[], index { 123 }
stub :[]?, index { 123.as(Int32?) }
end
let(node) { Node.new }
it "stubs the mock" do
expect(node.text).to eq("bar")
expect(node).to have_received(:text)
expect(node[0]).to eq(123)
expect(node).to have_received(:[])
expect(node[0]?).to eq(123)
expect(node[0]?).to be_a(Int32?)
expect(node).to have_received(:[]?)
end
end If the method arguments have type restrictions, then the following could be used: stub :[], index : Int32 { 42 } This should be available shortly. |
Works like: stub :[], index : Int32 { 42 } Addresses #14
Wow, that was quick, thank you! I had a second thought about the return type, instead of an impossible type declaration we could use a simple optional argument like: stub :[], index, return_type: Int32 { 123 } Another thing, when writing those tests, I struggled with passing a let(text_value) { Random::Secure.hex }
mock XML::Node do
stub text { text_value }
end This was raising an error because it couldn't find |
That's a good way to specify a return type. And yes, that is a known limitation (probably not documented though). The block for mock stubs runs in the context of the mocked type. It can't see anything in the spec, including |
What is your opinion on the stub block? It could be executed in scope of the spec or the mocked type, but not both. Which would be more beneficial for you? |
I think that when we're defining a block we're implying that this block is run within the current context (aka the spec). Because this is what happens usually. Do you have a real world example of a stub that needs to run in the context of the mocked type? |
That's how I implemented it in Origin recently. Since it's an edge case, the syntax is not too obtrusive IMO. |
Thinking more on it, having the spec scope would be better. There might be some technical problems to work out. |
The struct Node
def text : String
"foo"
end
def [](index) : Int32
42
end
def []?(index) : Int32?
42.as(Int32?)
end
end
Spectator.describe Node do
mock Node do
stub :text, return_type: String { "bar" }
stub :[], index, return_type: Int32 { 123 }
stub :[]?, index, return_type: Int32? { 123.as(Int32?) }
end
let(node) { Node.new }
it "stubs the mock" do
expect(node.text).to eq("bar")
expect(node).to have_received(:text)
expect(node[0]).to eq(123)
expect(node).to have_received(:[])
expect(node[0]?).to eq(123)
expect(node[0]?).to be_a(Int32?)
expect(node).to have_received(:[]?)
end
end I will create a new issue for changing the scope of stubbed methods. |
House keeping 🛎️ Please reopen if this issue hasn't been addressed or suits your needs. Stubbed methods can be a symbol ( This syntax has been quite clumsy. I'm planning to revisit the mock/stub DSL in an upcoming version. |
It would be great to be able to use symbols as stub names for mocks.
But I know this a complicated edge case ^^
My specific need comes from my Origin shard (https://github.com/pyrsmk/origin) which allows the delegation of methods with complex names like
[]?
or<<
. When implementing it in my code, I want to test that the delegated methods are well-delegated ^^Here's a concrete and simplified example:
There is a strong limitation (coming from Crystal): we cannot define those methods with a return type. So the only case of such implementation would be with mocks but not doubles (or only partially).
The text was updated successfully, but these errors were encountered: