rails 6 upgrade and FixtureSet seems fail - ruby-on-rails

Upgrading from Rails 4 to Rails 6.
All the cucumber tests start to fail after upgrading.
At the beginning of all the test scenarios, i am using
ActiveRecord::FixtureSet.create_fixtures(fixtures_folder, fixtures)
to generate the mock test data. And I tested the code with rails console, i can successfully get all the table/data in fixture files.
However, if I start a real scenario test run, the test is not able to find the related data in table. (actually the table is empty at all)
i tried
pp(ActiveRecord::FixtureSet.create_fixtures(fixtures_folder, fixtures))
to make sure the fixtureSet works before each scenario.

Related

Testing legacy app in RoR - skip the DB tasks

I'm a PHP dev mostly and just starting out with Ruby, so please correct me if I say something dumb.
I'm working on fixing some bugs in a "legacy" app written in rails. The app itself has never been unit-tested. I can see the test scaffolding generated by rails but tests are nowhere to be found.
The app is pretty big and the code quality is very bad, so I wanted to write some units tests for the functionality I'll be fixing, before writing any code.
The problem is that when I run rake test command, the testing DB is created, but if I write any tests it keeps crashing on me. There are several problems with some relations and keys which I tried to fix, but more problems just keep appearing. I do understand that the DB is created with schema.rb file, but I'm sure it is just outdated by now. It is another issue I will maybe fix, but for now I just want to write some basic unit tests not even using the DB itself.
So the question is: is it possible to write just unit tests for some methods without invoking all the test DB scripts? I'm aware that this is maybe not the best practice, but I will feel better modifying the app with some test coverage and starting with fixing the DB I do not yet understand seems like a bad idea to me.
I'm using Ruby version 2.1.10 and the app is written in Rails 4.0.4 - these seem to be the latest versions I managed to run the app on.

Run controller test on two different Rails applications

I'm helping to migrate a Rails 2 app to Rails 4, and I have come in towards the end of the project. The Rails 2 app has no tests.
Obviously we need tests to make sure the Rails 4 app works, but we don't want to write the tests in Rails 2 syntax, and then go through the pain of migrating the tests to Rails 4. We currently use RSpec on other projects.
I thought of writing them in Rails 4 as feature tests, and using Capybara.app_host to re-route the requests to a running Rails 2 application instance. Problem is, that the application is an API, and Capybara is not meant for testing APIs.
Is there any way of routing RSpec controller tests to another server instance?
Thanks again for the comments.
I think we have a plan. We tried to get cucumber and cucumber-api-steps running on our old project, but we realised that it was going to be hard work getting old gem versions to work nicely together.
The solution I'm trying now is using RSpec 1.3 and creating a spec/requests folder and adding controller tests to it.
I got this idea from a post from http://matthewlehner.net/rails-api-testing-guidelines/ - thanks Matthew. I don't see request specs in the docs until RSpec 2, so we make our controller tests pretty close to integration tests using integrate_views, then check the contents of the body returned by the controller.
As for the syntax, maybe we can include an expect(..) helper to return an object that calls #should from a #to method in the Rails 2 project and remove it when we port the Rails 4 project.

Why "rake test" and "rake test:functionals" give different results?

I've had a weird event of having rake test and rake test:functionals producing different results on a Rails 3 app that uses fixtures to insert test database records. The first group was failing on one of the controller tests, while the other one was successfully passing.
I also tried to run the failing test with ruby -I test path/to/file and zeus test path/to/file, but those runs were successful. The issue was reproduced on different machines. The test was failing because of the state of the test database, which seemed impossible to me when I looked at the fixtures.
Strangely enough, the issue disappeared the next day and I no longer can reproduce it. What could be the cause of such a problem and how to avoid it?

Test Unit Capybara not able to get query last inserted record

I am using test unit with fixtures using the standard rails testing framework. I am running some tests using capybara and I am unable to find the last inserted record when I run my model find after the insert. I open up my database editor and I see the record did get inserted properly. I can't figure out wether it's in a transaction and there is a method to get at that data or if it's even possible to get at it. I have even tried using raw SQL from my debugger window and that doesn't work. Any suggestions would be great.
The standard rails testing framework runs each test in a database transaction. As soon as the test is complete the data changes are rolled back. This allows each test to start with the same data, and allows the tests to be run in a random order to ensure you don't create any dependencies between tests.

how would I go about 'starting over from scratch' with Rails tests?

I have an existing Rails app that I built using Rails 3, Mongoid/Mongodb and Devise. The app is running fine. I'd now like to add some tests to it (sure, shoulda done this in the beginning but the learning curve for just Rails was enough...).
I've used several pages to get it going, especially the Rails guide and this blog post about Mongo and Cucumber/Rspec. My concern here is that between all of the "add this to this and such file" that I've done to try and get this working (and it's not) I've made such a mess of things that it might be better to start over from scratch. With the testing portion of the app.
I thought I would just delete the spec and test directories and re-gen the tests but I can't find a command to do that (the regen).
I've built a very simple test (assert true) but I'm getting:
D:/Dev/TheApp/test/test_helper.rb:10:in `<class:TestCase>':
undefined method `fixtures' for ActiveSupport::TestCase:Class (NoMethodError)
I think the real issue here is that I'm using MongoDb and the test architecture in Rails seems to really really want to do ActiveRecord. Not sure if those two are compatible.
Is there a quick way to build a barebones test directory? My short term solution is to just roll back those directories. Hoping for a better solution.
The blank tests are really worthless. If you didn't have tests/specs of value, then just start from scratch. And if you want to start over, you should just delete them and start new.
You could treat your code as "legacy code" as defined by Michael Feathers in Working Effectively with Legacy Code -- that is, code without tests.
Take a look at this getting started with rails testing guide over at 10gen:
http://www.mongodb.org/display/DOCS/Rails+-+Getting+Started#Rails-GettingStarted-Testing

Resources