mamba icon indicating copy to clipboard operation
mamba copied to clipboard

How to achieve something akin to rspec let

Open caldwecr opened this issue 10 years ago • 4 comments

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.

caldwecr avatar Feb 11 '15 06:02 caldwecr

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!

nestorsalceda avatar Feb 12 '15 07:02 nestorsalceda

Thank you. I really look forward to the continued enhancement of mamba.

caldwecr avatar Feb 12 '15 18:02 caldwecr

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!

cjbottaro avatar Apr 27 '16 02:04 cjbottaro

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:

nestorsalceda avatar Apr 27 '16 10:04 nestorsalceda