How to defrost mocha?

I have the next mocha mok that works great.

In the test.rb file:

setup do Date.stubs(:today).returns(Date.new(2011, 7, 19)) Time.stubs(:now).returns(Time.new(2011,1,1,9,0)) end 

The problem is that the test time is interrupted. After running the tests, date and time objects are still mocked. (!)

Finished in -21949774.01594216 seconds.

I added the following:

 teardown do Date.unstubs(:today) Time.unstubs(:now) end 

This causes the following error for each test: WARNING: there is already a transaction in progress

Is this a suitable way to decouple? Is it better to understand at the end of the test file or even at the end of the unit test package?

Work in Rails 3.07 and Mocha 0.9.12

Thanks.

+4
source share
2 answers

I do not know if this is your problem completely, but it is just unstub , not pluralized.

In addition, there should be no problems. You definitely want to unwind after each test (or a set of tests, if a bunch of tests need a break), because as soon as it is shaded, it will remain pointed, and this can ruin other tests.

+9
source

The accepted answer extends to misinformation and should be considered harmful.

One of the main goals of a mocking library, such as Mocha, is to automatically disable the layout / stub as part of integration into various test libraries. In fact, if you look at the GitHub repo for Mocha , you will see that significant maintenance efforts are aimed at making Mocha work smoothly with all versions of several different testing frameworks.

If this does not work properly, you need to find out why Mocha's built-in stall does not work. A manual mismatch in your own break is just a paper problem and can mask more subtle problems with a stub or Mocha leak, otherwise the behavior is wrong.

If I had to take wild money, then the money on your stub would somehow run outside the actual test, because this is the most common reason that I saw for this kind of thing in the wild, but there is not enough information from the question to really install.

+5
source

All Articles