This is maybe a bit of an odd question, i've googled around and haven't seen any questions asked (But maybe im asking the wrong question).
So i've been rewriting Integration tests using Factories (Which is interesting, and thanks to the fine people at this site for your help!). But im pretty much relying on screenshots to see that what im wanting to happen in the tests is happening.
Im using a Test database (SQlite to be specific), and since im using Factories the data gets deleted of course once the test is over, so it's like it never happened. So running a server using the test environment I can't really "manually" verify the data.
Is there a good way to be able to manually verify the data? Like stopping the test RIGHT at the end or pausing it temporarily? I suppose I could put in a gigantic sleep...but surely there is a better way?
But a breakpoint in your test, then use whatever commands you like to query the db.
I use gem 'pry-rails' in my Gemfile
then you simply put the line binding.pry in the test and it will freeze there in the terminal and you can check variables, make db calls like User.all or User.count and if you also add the gem pry-nav you can navigate with next, step, continue etc
Related
I'm using observers in my Rails app for notifications, activity feeds, etc. They keep my models and controllers clean, work great on the app side, and in any non-integration test (I've unit tested them extensively/successfully). But I can't for the life of me get them working in integration tests. I have the following in spec/support/observers.rb (as a sanity check. I'd ideally only enable them for feature tests if I can get them to work at all):
RSpec.configure do |config|
config.before do
ActiveRecord::Base.observers.enable :all
end
end
No matter what I do, I can't get my observers to fire off during my integration tests (which is exactly what I really want to use to ensure that everything is truly working).
Does anyone have any insight on this or have any clue as to why I could be experiencing this? I'm using Rails 5.0.0beta3, and the latest versions of rails-observers, rspec-rails and capybara (from github master branches).
This isn't truly an answer to my question, but ain't nobody got time to wait around. No one cares about observers anymore because they were removed form Rails 4. There are next to no google topics related to them and capybara.
So, in order move on and get integration test coverage on the code I need, I'm refactoring my code to use concerns that include callbacks. This eliminates the observer dependency, and I was actually able to abstract my code even further this way since many of my observers were doing pretty much the same thing, and I had the core of what my observers were calling in service objects anyway.
Integration tests have no problem triggering these.
RIP Observers.
https://stackoverflow.com/a/18581715/2202674
I'm a fairly experienced dev but I'm a total web and rails newbie. I'm trying to implement a kind of market place rails application. I've developed the app to where I'm far along in the customer interaction flow and now I'm realizing I need to automate stuff cause dev/test via browser consumes so much time.
Say I have customer interaction flow that goes from page A, then to page B, C, D...G. And now I'm developing the 'H' page. I know I could use the automated test facilities (e.g. Minitest) to automate the whole thing. But being as how I still have my rails training wheels on sometimes I like to see database state progress from one state to the next for myself rather than just trust that the test automation really is doing what I'm expecting.
My question is, is there a way to have Minitest automate/simulate the user interaction flow from A -> G and then let me take over in a manual way from there such that I could click my newly-developed browser button while using my db browser to watch db state progress appropriately?
If not - which is what I'm guessing - I'd happily settle for a way to examine the database contents after an integration test. My integration tests work just fine. It's just that after they complete I'd like to be able to pop open my db browser and poke around, verifying various values to ensure to my satisfaction things in the db are as they should be.
Another possible solution that would work for me is if there was something within my integration tests that could access db values so I could verify things programmatically. I have in mind something like how assigns give you access to instance variables within your integration tests that you can then do 'asserts' calls on.
And, of course, please let me know if I'm way off track and there's a "best practice" for this type of thing that I've missed.
I'm using Rails 4.2.0 and Ruby 2.2.2p95
While Minitest doesn't have a way to do the first option you mentioned, you might checkout Watir as a means of (partially) automating tests so you leave off at a certain point with the browser still open and continue manually while watching what's going on in the DB.
However, since the second option you mentioned is possible with Minitest (and will save you even more time by being fully automated) I'd highly recommend it. See the Ruby on Rails Tutorial 3rd Edition by Michael Hartl for some examples (you might find the whole thing helpful, but here's a section that shows a test much like what you are looking for: Section 7.4.4.)
I've got a ruby on rails application with significant database seeds that tie into the UI elements and are thusly required for integration tests.
I'm using database_cleaner to clean up after my tests, but due to the heavy js use of the application most of my tests can't be run with the transaction strategy which allows me to specify tables to leave out of the rollback.
The result is that I've got to re-seed the database before each test or deal with a dirty database. Does anyone have tips for testing in this kind of situation or know of any tools that may help?
Thanks!
I've dealt with this in the past by using transactions for our tests (we're using something a little homegrown for that at the moment but previously we used database cleaner).
The key is forcing everything to use the same database connection. The path I've been down for this is to set the database pool size in test to 1: everyone has to share the same connection because active record won't let you create more connections.
For this to work you need to be careful about releasing connections no longer in use, by calling ActiveRecord::Base.clear_active_connections!
There are 2 places you need to do this
when your test code is about to call visit, click etc.
after each request (use a Rack middleware for this- you can basically copy ActiveRecord::ConnectionAdapters::ConnectionManagement)
This works for us writing specs against pages that use ajax to fetch intermediate data and so on, but can be a bit tricky to get working initially. I wrote about it more extensively a while back
I'm new to testing, and having troubles debugging like I might normally in a model or controller.
I've created a user from a factory (using FactoryGirl, if that makes a difference), and I'm pretty sure the create method is failing because of validation when saving. However, I'd love to know how to debug a model instantiated during testing.
I've tried:
user.inspect
puts user
raise user.to_yaml
(The latter works, but stops execution of the rest of my tests, and doesn't show validation errors--it only proves the existence or non-existence of the model I tried to instantiate.)
Other than raising the model as an error, there is no debug output during testing, and the only other thing I've been able to do is tail the log for my test DB and see what's happening there, but it seems clunky at best. What methods would you suggest for accomplishing what I'm after?
Thank you for any direction
Got the answer, it couldn't be easier (this user phrased my question much better and more concisely :)
How do I output a variable in a rspec test?
short answer, use pretty print:
pp user
Checkout plymouth: https://github.com/banister/plymouth
It works with PRY to give you a nice REPL & debugging interface when a test fails. I've used it on some projects and found it very very handy for obscure issues in tests.
You can always crack open a pry session by adding binding.pry to your test. That way you can have access to whatever is available at the exact point in the test (and elsewhere if desired). More info on adding pry to Rails in the official Pry wiki
I am testing a Rails 3 app with a Cucumber/Capybara combo. I am also trying to use Selenium to test some JavaScript specific scenarios but am running into weird difficulties I don't understand.
My experience with Cucumber/Capybara is pretty low, my experience with Selenium is zero.
Here's the scenario:
Scenario: Browsing events
Given many events exist
And I am on the events page
Then I should see a list of 15 events
When I follow the first event
Then I should be on the event page
And I should see a google map
And I should see the event details
When that scenario runs under RackTest, it passes all the way up to the Google Map step, at which point it fails because there's no JavaScript. This is expected.
When I run the test with the default JavaScript driver (Selenium) it fails on step three (I should see a list of 15 events). When I observe the browser window, indeed the list of events contains no events at all - almost as if they don't exist in the database.
Incidentally, the first step (many events exist), uses FactoryGirl to create a load of events.
As this is all pretty new to me, I wonder if I'm being caught out by a typical gotcha? I haven't made any configuration changes other than the standard install from rails g cucumber:install. Also, if it's relevant, I'm using OSX.
Thanks
I presume you are trying to use transactional fixtures (the default behavior) with Selenium, but that won't work. The transaction that is managed within the test is out of scope when the browser invokes the Rails app separately, so it can't see any of the uncommitted data that your test has created.
Instead of transactional fixtures, you'll need to use one of the database cleaner gems.
https://github.com/bmabey/database_cleaner
Edit:
I subsequently became aware that it is possible to use transactional fixtures with Selenium tests, and performance is better if you do (credit to Kira Corina's answer). See http://pastie.org/1745020 for details.
For those who have the same issue with database in selenium tests, here is a really useful chat with three basic solutions summarized by Jonas (see the first April, 5th message):
https://groups.google.com/forum/#!msg/ruby-capybara/JI6JrirL9gM/R6YiXj4gi_UJ