I am going through Hartl's rails tutorial and the autotest is no longer running newly written tests. I write a new test and it will not be reflected in autotest. Autotest still runs and remains green with a set number of examples. Existing tests will fail if I change things but new test are not recognized. Any ideas? Thanks.
Caught it. I had an errant end keyword.
Related
I am running Ruby on Rails 5.1.4 and am trying to upgrade to Ruby 2.5.1 from Ruby 2.4.4. When I run Rspec, it is running incredibly slowly. I have used the method described here by myronmarston allows me to see what is happening.
The Rspec program appears to be progressing normally, just every step is taking a long time, for example it takes over half an hour to finish the rspec.configure block in spec_helper, minutes to execute a validate statement in a model. I have tried different tests and it is not any specific test code.
If I start up the rails server, the app appears to be working normally. The console appears to work normally as well, it is just rspec. I have tried spring stop and it makes no difference to outcome.
Any ideas on what is causing this?
I got a strange problem with minitest and capybara.
I am using rails 3.2.8 and test with minitest/capybara/poltergeist. Until now every went fine. I always could test my javascript stuff.
For a new project I downloaded rails 4 to get into it a little bit. And since minitest will be the testing framework I thought it would be easy. It was not. Truth be told, I am not a hero when it comes to setting up all the stuff. I just follow Ryan Bates. After a lot of adding and removing and updating a lot of gems I decided it wasn't worth to continue to use Rails 4. I had so many issues with getting into the groove with my integration tests. All the stuff I knew did not work as expected. The axe fell when almost everything worked until I wanted to test a javascript thing. I got this error:
.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/minitest-4.6.1/lib/minitest/spec.rb:190:in `it': wrong number of arguments (2 for 1) (ArgumentError)
because of this
describe "blabla" do
it "does not do what i want it to do", js: true do
pending
end
end
It will not accept the js: true argument. Funny thing is that the describe block will accept the js: true argument.
When I went back to Rails 3.2.8, because I thought it was a Rails 4 thing, this baby followed me right into a new testsuite. I tried hard to find an answer on Google but I can't find any. My other rails 3.2.8 projects still test fine, no complains about the javascript argument. But with the new apps: no javascript testing.
I am at a loss here. I have no idea where this is coming from. Since my other 3.2.8 apps still work fine, it has probably something to do with renewed gem versions? Has anybody seen this error message? I checked the complaining minitest/spec.rb file from the error message, line 190 for several minitest versions and nothing changed in the it-method.
Please let me know if you want to see stuff (Gemfile? test_helper.rb?) if you have any clue about what might be wrong. Thanks in advance!
Casper
Minitest's spec DSL does not accept a second parameter for the it blocks. The minitest-metadata gem adds support for the second argument, and the example shows how to configure Capybara to use it. Perhaps your existing projects use minitest-metadata and configure Capybara with it, and your new projects don't?
When running rspec tests on my rails app using "rake" or "rake spec", it takes a long time to initialize Rails and start running tests.
Is there a way to keep Rails loaded in memory and run tests against it? I'm hoping there's something similar to using "grails interactive" like this How to speed up grails test execution but for Rails.
It's almost what Spork is all about.
See the most awesome railscast "How I test" http://railscasts.com/episodes/275-how-i-test
This uses guard to run a relevant test every time you save a file so you know almost instantly if the code you have written has caused a pass or a fail. Also if you are running on linux then you can use the libnotify and libnotify-rails gems which pop up a window for you (totally unobtrusively) indicating a pass or failure so you don't have to keep checking the console.
There is a Railscast called Spork that specifically discusses this problem at http://railscasts.com/episodes/285-spork and offers an excellent solution with a detailed walkthrough.
My trouble arose in Chapter 11 of the Ruby on Rails Tutorial here.
I was seeing this rspec error:
Failure/Error: :user => Factory(:user, :email => Factory.next(:email)))
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken
first in user_spec.rb then in micropost_spec.rb. It was pretty puzzling. I thought the factory statements were generating a user in a fresh testing db each time autotest ran rspec. I checked out source files from the master branch with git and tried again, but saw the same error. I therefore suspected it related to the db contents somehow and not the code.
So, I did the following:
restarted "rails s"
restarted autotest
rake db:reset
rake db:migrate
rake db:test:prepare
rake db:populate
... and it all went green. The rspec tests passed.
There may be a more “to the point” solution, but I was thrilled this worked. Hope it helps someone else. I am left to conclude that my testing/development somehow added something to the db that was unexpected. I suppose the above steps are a good way to make yourself a fresh db near the end of chapter 11.
Was there a more direct way to solve this? Does the error indicate some other issue that I addressed without realizing it? I am left thinking that running rspec does not guarantee a fresh testing db each time. Is that a wrong assumption?
I had trouble with the Integration tests shortly after Chapter 9.4. All of my controller tests and the request integration test blew up with the message 'Email has already been taken'
What I learned from RailsTutorial - chapter 8.4.3 - Test database not clearing after adding user in integration test is that you need to do something to clean up after integration tests, because unlike unit tests they may not clean up after themselves.
The solution presented there was to use the DatabaseCleaner gem, the implementation of which is also explained in the linked Question.
I think that if you don't implement some strategy for cleaning up after the integration test you will continue to have to use your 'shotgun' solution for cleaning up the DB every time you run the test suite. Definitely not fun.
This works for me:
bundle exec rake db:test:prepare
It's also in the tutorial.
For me, the issue seemed to be running Spork. After restarting it I can run the tests as many times as I need.
It helped, added to the file factories
sequence(:email) {|n| "person#{n}#example.com" }
In my case problem was in equals emails in
factory :user
and
factory :user_with_additional_options
I have been going through Michael Hartl's tutorial http://railstutorial.org/ and for the most part it has been a huge help in getting started with Rails. The book is very focused on TDD, which is great because I wanted to learn TDD, but the problem is 90% of my tests fail with the error "Email has already been taken". I think what is happening is that when the test runs it creates a user with email "user#example.com" as suggested in the book. The problem is when the second test runs which needs to create a user, it is using the same "user#example.com" email address. I know there are workarounds I've seen using factory girl to create a sequence of email addresses but I shouldn't have to do this to get the example from the tutorial working properly.
Has anyone else run into this problem? Judging by the lack of questions on this particular topic I am thinking it is a bug in my code but maybe someone else encountered this.
Doh! The problem was a commented line:
config.use_transactional_fixtures = true
in spec_helper.rb!
Newbie mistake.
An useful asset to clean your db for yours tests:
https://github.com/bmabey/database_cleaner
You should search on topic to how to clean database after each test. Issue may be as well caused by default user you load from fixtures and then when you create hin in the test again, but if you say 90% it seems like the first case.
I do not attach links because it makes a difference when you use rspec, test::unit or cucumber, each of them has its perks.
Happy coding!
I've had this problem recently as well. The test DB should be cleaned after tests but for some reason it wasn't. All I did was run 'rake db:reset' to reset everything and then make sure that I was using factories (Factory Girl). Just try using either fixtures or factories so you know for sure you aren't actually hitting the database.
I saw similar behavior near the end of CH 11. My spec_helper.rb was correct. This fixed it for me:
restarted "rails s"
restarted autotest
rake db:reset
rake db:migrate
rake db:test:prepare
rake db:populate
HTH,
Perry