I am using Ruby on Rails 3.0.7 Cucumber 1.0.1, and Oracle as the database. I have my own local user/schema for my development database, but I also connect to another Oracle database in a read-only fashion. Someone as my work has created a gem that allows me to talk to that other database and access its models. Let's say we call that other database Servo. Then I am able to do something like Servo::ModelName.all and all other regular calls.
The Oracle that gets generated by that is something like: SELECT * FROM "SERVO"."MODELNAMES"; Note that it appends the SERVO database name.
Now, I am writing a Cucumber test that at times, in my custom steps, uses Servo. That all works fine. The new scenario I am writing uses the #javascript tag. If any step (in the Background or in the Scenario) does not use Servo, then it runs fine. However, if I do use Servo, it breaks. The Oracle SQL being generated is suddenly SELECT * FROM "MODELNAMES"; and it is no longer aware of the other database.
So, any ideas why this is happening? Why does the #javascript suddenly change the SQL being generated? I couldn't find any good documentation about what else it does besides launching Firefox.
I'm sure others may want to know more information but I didn't want to flood this question so just let me know if you want some more details.
The main difference #javascript will introduce on the application side is that you're running a web server instead of routing requests directly through rack. I'd investigate with that knowledge and see if there isn't something you're doing in an initializer or something which could be affected by the different context.
Related
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 currently creating rspec test cases for a Rails 2.3 application. The problem I'm confronting is that the application has a dependency on data from an external database like this:
Main_User is an external application.
Node_User is the application I'm testing.
When a new Node_User is created, Node_User calls Main_User which creates a user and returns with some data which Node_User uses to create a the Node_User.
Is there any way I can query the Main_User database to verify that the record created there contains the data that is passed to Node_User? Or is there a better way to test this?
Thank you!
Yes, you can theoretically query the remote database if you have access to the user/pass/hostname for that database. (Search for "Rails multiple databases.")
But I wouldn't necessarily recommend taking that approach. Your tests would be modifying the state of the remote database. This strikes me as unsafe. You'll be creating bogus records--and possibly deleting or corrupting data, if your tests do any mass deletes or exercise broken functionality. In other words, your tests may do bad things to a production database.
Therefore, I would recommend mocking the remote database in some way. One option would be VCR. It's really cool, but it would require you to operate on the remote database at least once, because it has to record the first run. Another option is to use one of the tools underlying VCR, such as FakeWeb.
Of course, this only tests the Node_User side of things. You said you also need to verify that the remote record is correct. To do that, I would recommend writing tests on the Main_User side. Such tests would simulate an incoming RESTful request from Node_User and verify the correct result.
That only works if Main_User is your application. If it's a third-party service, then there may not be a safe way to test it. You may just have to trust its correctness.
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
So for a class I have to turn in my Rails application to my professor. What is the best way to make sure everything goes smoothly when he trys to start it up? Also, is there anyway I can freeze a database and send that with it so he has all of the data I have been using in the application?
Thanks a lot.
Depending on your needs, the SQLite3 database (used by default in Rails) is stored on the file system in the db directory of your Rails app. So, assuming your professor has the requirements to run Ruby on Rails, the application will start up with the data you've used.
My guess is you have hard coded connection strings in your rails application. Ask your professor what server he will be running it off of. At that point either change the strings to match or create a config file that is read in and can be edited (which is the better choice of the two). Most databases have export functionality which will allow you to export the current information within the database.