mamba
mamba copied to clipboard
How to achieve something akin to rspec let
I am totally new to Python and Mamba (from Ruby). So far I'm actually quite pleased with the rate I have made progress with my very limited Python knowledge.
(Please note this is specifically about manipulating the state of the subject of the example before executing the method under test - as such it isn't conveniently accessible in before/after hooks and shouldn't be there anyway rspec hooks )
In rspec I'm used to doing something like this ...
describe '#shiny?' do
let(:cat) { c = Cat.new; c.make_cat_shiny!; c }
subject(:mut) { cat.shiny? }
it "is a shiny cat" do
expect(mut).to be true
end
end
There isn't (from what I've seen) a direct equivalent in mamba. So far the most similar and yet some what idiomatic solution I've come up with in mamba is like so...
with description(Cat):
# self.subject is an instance of Cat
with description('#is_shiny'):
# self.subject is (still?) an instance of Cat
def test_cat(self):
t_c = self.subject
t_c.shine()
return t_c
def mut(self):
return self.test_cat().is_shiny()
with it("is a shiny cat"):
expect(self.mut()).to(equal(true))
Is there a better / more idiomatic / canonical approach or convention for accomplishing let?
What's the proper way to set the subject, when the subject is a method?
I saw something in the spec path that looked relevant and started me down the path I've highlighted above.
Hello!
The let feature is not implemented, I "emulate" that behaviour with before/after hooks and using self. Another way would be using properties.
Anyway, I will work on the let feature if you think is valuable.
Thanks for the feedback!
Thank you. I really look forward to the continued enhancement of mamba.
I "emulate" that behaviour with before/after hooks and using self. Another way would be using properties.
Can you give an example of both those emulation methods? I'm new to Python (coming from Ruby and RSpec) and appreciate any tips (with examples).
Thanks!
Sure!
Let's take the example in first comment :)
with description(Cat):
with context('when meows'):
with it("makes a lovely noise"):
cat = Cat('Haskell')
expect(cat.meow()).not_to(be_none)
And you can transform it to:
with description(Cat):
with context('when meows'):
with before.each:
self.cat = Cat('Haskell')
with it("makes a lovely noise"):
expect(self.cat.meow()).not_to(be_none)
Internally, mamba creates a class for each description and context, and you can use self for keeping references to those instances.
Those days I'm quite busy, but I really appreciate your comments dudes :heart_eyes_cat: