Code Monkey home page Code Monkey logo

Comments (4)

floehopper avatar floehopper commented on May 13, 2024

Hi Jakub. Interesting idea. However, I can't really see why it would be useful. Why wouldn't you just split the test into two tests with a different expectation in each? Do you have a concrete example where this idea would be useful? Thanks for your interest in Mocha. Cheers, JM.

from mocha.

farnoy avatar farnoy commented on May 13, 2024

Hi, sometimes when I call two methods that call another one, I have to set the .times() for the expectation, and it's not specific as to which method calls the other one, for example

  • a calls c once
  • b calls c twice
  • .expects(:c).times(3)

when a and b change, a can call c three times and b can never call c, so the behaviour is very different, but the test still passes.

I guess splitting the test is a good way to go, but I was just curious if there's a possibility in mocha right now for that.

from mocha.

floehopper avatar floehopper commented on May 13, 2024

So if I understand you correctly, you're saying that given a class and test like this :-

class MyClass
  def initialize(collaborator)
    @collaborator = collaborator
  end

  def method_one
    1.times { @collaborator.foo }
  end

  def method_two
    2.times { @collaborator.foo }
  end

  def method_three
    method_one
    method_two
  end
end

require "test/unit"
require "mocha"

class MyClassTest < Test::Unit::TestCase
  def test_method_three
    collaborator = mock("collaborator")
    my_object = MyClass.new(collaborator)
    collaborator.expects(:foo).times(3)
    my_object.method_three
  end
end

I can change MyClass#method_one and #method_two as follows :-

def method_one
  2.times { @collaborator.foo }
end

def method_two
  1.times { @collaborator.foo }
end

And the test will still pass. Is that what you mean?

I think the way it currently works makes sense to me. The test only cares how many times collaborator#foo is called it does not care which method calls it. If #method_one and #method_two call #foo with different parameters, then you might need to be more specific with your expectations, i.e. use Mocha::Expectation#with to constrain the expected invocations of #foo.

If you care about how many times #method_one calls #foo then maybe you should have a test that only calls #method_one and then you can have an expectation for a specific number of invocations.

You might also want to look at the Mocha::StateMachine examples e.g. Mocha::Expectation#when, Mocha::Expectation#then. This might allow you to do something like what you are talking about.

So far I haven't been convinced to make any changes to Mocha. If you are happy with the above explanation, can you close this issue? Otherwise you need to try harder to convince me that Mocha is missing some important functionality!

from mocha.

farnoy avatar farnoy commented on May 13, 2024

You did understand me correctly and both solutions that you've provided are great. I wasn't aware of the StateMachine implementation, I'll have to take a closer look at it. expect blocks really seem useless now.

Thanks for your time and a lengthy response

from mocha.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.