Skip to content
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

Expectation ignored when test cases are run in a certain order #298

Closed
skliew opened this issue Mar 27, 2017 · 6 comments
Closed

Expectation ignored when test cases are run in a certain order #298

skliew opened this issue Mar 27, 2017 · 6 comments
Assignees

Comments

@skliew
Copy link

skliew commented Mar 27, 2017

Found this issue using version 1.2.1. In the code example as follows, test_case_2 passes even when there is no call to any method named test1, resulting in a false positive:

require 'test-unit'
require 'mocha/test_unit'

class SUT
  def test1
  end
end

class MyTest < Test::Unit::TestCase
  self.test_order = :defined
  def test_case_1
    SUT.any_instance.expects(:test1).once
    SUT.new.test1
  end

  def test_case_2
    Object.any_instance.expects(:test1).once
  end
end

Do note that when the line SUT.new.test1 is commented out, both test cases fail. And when test_case_2 is run before test_case_1, only test_case_2 fails which should be the correct behavior.

@floehopper
Copy link
Member

@skliew: Thanks for reporting this issue. Setting an expectation on any instance of Object seems like a bit of a pathological case, but at first glance I agree the behaviour doesn't sound correct. We'll try to look into it as soon as we have some spare time. Out of interest, do you have some real tests which need to do this?

@skliew
Copy link
Author

skliew commented Mar 28, 2017

I actually found this problem in a real code base when a test case occasionally fails. I have to admit that the test case was itself buggy, as most of the time false positives are reported.

And thanks for the great library :)

@floehopper
Copy link
Member

@skliew Hmm. Sorry for taking so long to get back to you. I just did a bit of digging and it appears this might be a more fundamental problem with the Test::Unit integration. I'm continuing to investigate.

@floehopper
Copy link
Member

I take back my last comment about it being a more fundamental problem. I made the mistake of looking at this before I'd had any coffee! I'm continuing to investigate.

floehopper added a commit that referenced this issue Apr 20, 2018
Previously ObjectMethods#mocha & ClassMethods::AnyInstance#mocha were
building mock objects for ancestor classes when ObjectReceiver#mocks or
AnyInstanceReceiver#mocks was called from Mock#all_expectations as part
of Mock#method_missing.

Since these mock objects were not associated with a corresponding
ClassMethod or AnyInstanceMethod registered with Central via
Mockery#mock_impersonating or
Mockery#mock_impersonating_any_instance_of, they were not being reset as
part of Central#unstub_all via Mockery#teardown and this state was
leaking into subsequent tests.

You can see an example of this in #298. I've captured a more general
case of this in the new acceptance test method in
StubAnyInstanceMethodDefinedOnSuperclassTest which was failing before
the fix in this commit.

I've also added a similar acceptance test method to
StubClassMethodDefinedOnSuperclassTest which was also failing before
the fix in this commit.

I'm not particularly happy with the fix, but I think it'll do for now.
@floehopper
Copy link
Member

@skliew: I've opened #331 as an attempt to fix this problem. It turns out that it is indeed a bit more general than I first thought, so many thanks for reporting it!

@floehopper
Copy link
Member

@skliew The fix in #331 was just released in v1.6.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants