Rails spec pass example alone but fail the whole file - ruby-on-rails

I have one rails spec file.
When I run: rspec my_spec.rb
There were 3 failures. These failures are in the same context.
But when I run them seperately (Example: rspec my_spec.rb:231),
they passed.
Rarely individually test also failed, if that, I restarted docker then it passed again.
I am not sure what was wrong?
I tried adding DatabaseCleaner.clean and redis.flushdb in spec_helper but no lucky.
Any idea can help?

Is very difficult to help you without the specs.But based on my own experience, usually this is a problem with hardcoded expects. Like:
expect(response.id).to eq(1)
rather then:
expect(response.id).to eq(object.id)
When you run your test only it works because of there one entity only. Check this and if it don't help, please provide us more information. Glad to help.

Related

--seed option in RSpec

Is anyone able to explain what this actually means? The documentation seems to say that it is similar to setting an order (--seed 123 # same as --order rand:123), but from what I can tell doesn't seem to go into it any further. I'm assuming it's not related to database seed data, but I could well be wrong.
In the default configuration, RSpec runs its tests in random order.
This is considered a good practice because tests should be independent of each other. Running them in a random order helps to find tests that only pass when they are run in a specific order and fail in another order.
But the problem is: When RSpec runs the test in random order and then fails, how can you re-run the test in the exact same order again to debug the issue?
That can be done by telling RSpec to use the same seed for its randomness as it was used before.
RSpec tells you this seed when it is starting:
$ rspec spec
Randomized with seed 48111
.....*.........
To re-run the specs in the exact same order run:
$ rspec spec --seed 48111
Reading about Random#seed might be interesting in this context.

RSpec "retest broken tests from last time" flag

Is there any way to retest previously broken tests?
So, say, I run rspec and several tests in different files and directories fail.
I fix something and now I have to manually specify all files and folders I want to retest or run tests for whole project again(It takes considerable amount of time for big projets).
What I was looking for is something like a flag
rspec --prev-failed-only
I realize that such flag would require considerable amount of additional actions from rspec, like storing results of previous tests and so on. But I think it would be super convenient to me.
Is there any such(or similar) tool/gem?
rspec-rerun gem does what you want : https://github.com/dblock/rspec-rerun
https://github.com/rspec/rspec-core/issues/456 has a good discussion on the topic of making rspec itself be able to rerun failed tests.
On the Giant Robots podcast, Sam Phippen of the core team mentioned this feature is due to be added to RSpec soon.
In case anyone else finds this, a month after the question was asked (open source <3 ) rspec 3.3 introduced the --only-failures option, also the delightfully handy --next-failure (-n) option. rspec --help for more info.

rails test database not clearing after some runs

I am using rspec to test and I notice that after running the tests, it sometimes leaves some records in the test database. I'm not sure why. I have use_transactional_fixtures set to true in my config file. But they don't go away until I manually delete the records. Does anyone have a way to stop this?
EDIT; before i said it was only when tests fail. that's no longer true.
I found the problem. Before(:all) blocks are not transactional
Try database_cleaner gem.
Truncation or Transaction strategy will work for you.
Caution: It can make your test suite run terribly slow.
Normally, proper use before, after in RSpec(if you are using it) works usually fine.

How to unstub Mocha mock?

I have the following mocha mock that works great.
In a 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 timing is broken for the tests. After the tests run the 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 throws the following error for each test: WARNING: there is already a transaction in progress
Is this the proper way to unstub? Is it better to unstub at the end of the test file or even at the end of unit test suite?
Working in Rails 3.07 and Mocha 0.9.12
Thanks.
I don't know if this is fully your problem, but it is just unstub, not pluralized.
Other than that, there should be no issue. You definitely want to unstub after each test (or set of tests, if a bunch of tests need the stubbing) because once stubbed, it will stay stubbed, and that can screw up other tests.
The accepted answer is spreading misinformation and should be considered harmful.
One of the main purposes of a mocking library like Mocha is to provide automatic mock/stub teardown as part of the integration to various testing libraries. In fact if you look at the GitHub repo for Mocha you will see that significant maintenance effort is put into making Mocha work smoothly with all the versions of several different testing frameworks.
If this isn't working properly then you need to figure out why Mocha's built-in teardown isn't working. Unstubbing manually in your own teardown is just papering over the problem, and could hide subtler issues with stub leakage or Mocha otherwise misbehaving.
If I had to take a wild guess money would be on your stub somehow being run outside of an actual test because that's the most common cause I've seen for this kind of thing in the wild, but there's not enough information from the question to really ascertain.

How can I have autospec/test not run the full test suite after everything goes green?

Same question as waloeiii in twitter:
How can I have autospec/test not run
the full test suite after everything
goes green? The next test I write will
be red!
I'd rather run the full test suite manually.
By the way, I tried adding a failing spec:
it "should flunk" do
flunk
end
but autospec seems to ignore it when it feels like it.
Bit late but I was looking for this as well so thought I'd post my solution:
Add the following to ~/.autotest:
class Autotest
def rerun_all_tests
end
end
Are you sure you are not confused about the intended behaviour of autotest's heuristics?
My understanding is that it runs tests for what has changed and will keep running failed tests until they pass and then once they pass it runs the whole test suite to make sure nothing else broke.
In effect it is being conservative and making sure you haven't introduced side effects that break other unrelated tests which is probably a good thing. The problem of course is that if you are doing fast red - green cycles you are going to be running your full suite a lot.
If you want to change these behaviours you need to edit the heuristics in the rails_autotest.rb file for zentest.
You can use the following option to avoid this behavior -
autospec --no-full-after-failed
I think that this is by design - if you fix a failing spec, and all other specs in the section are green, then autospec will rerun the entire suite - this will tell you if the fix you applied to one area of your project has b0rked another or not.
If you just want to run the specs you are working on at any one time, then you can do it from the command line:
ruby spec/controllers/my_spec.rb
or from within Textmate by pressing cmd+r from your spec file. You should rerun your entire suite as you go anyway, otherwise you might be missing failing specs.

Resources