Rails rspec test fails when switching from sqlite3 to mysql2 - ruby-on-rails-3.1

I have a small rails application, where I had originally been using sqlite3, then decided to switch to mysql. After I switched, one of my rspec tests broke, but I can't tell why (it was working fine when using sqlite3, and checking the functionality via the actual page appears to work correctly).
My tests are as follows:
before(:each) do
#a = FactoryGirl.create(:a)
#b = FactoryGirl.create(:b)
#relationship = #a.connector.build( :b_id => #b)
#relationship.save
end
Test 1:
it "should have the right b" do
#relationship.b.should == #b
end
Test 2:
it "should have the right a" do
#relationship.a.should == #a
end
In my tests, test 1 fails, while test 2 passes successfully when using mysql (both path when using sqlite3).
As far as I know, I shouldn't require any changes to the tests themselves if I am just switching databases, so can anyone tell if I am doing something wrong? Or if I need to add something to refresh the databases?
On a related note, what is the recommended way to debug issues in the rspec tests themselves, especially if the actual development works, but the tests fail?
(I am using Rails 3.1 and rspec-rails 2.10.1).
Thanks in advance for any input,
Regards,

I recently ran into this issue; the problem was being caused by buggy caching behaviour in my model.
In the first spec, I was caching the first object, which was created with ID 1. When recreated for the second spec, the object was created with ID 1 on SQLite3, and with ID 2 on MySQL.
So, the caching behaviour was working by luck when tested on SQLite3, and failing consistently on MySQL.

Related

Annoying Guard notification when testing

Recently I made a simple ruby application and have been using minitest to test it.
Following the advice of the Head First Ruby book, I automated this testing using Rake(I'll write what it told me to put in the Rakefile at the end of this post, in case that helps). The test seems to run fine (everything passes in a way I would expect it to), but I always get this notification at the end of it all:
rvm/gems/ruby-2.3.0/gems/guard-2.14.0/lib/guard/notifier.rb:28: warning: instance variable #notifier not initialized
Testing things manually by telling ruby to include which files I want, does not have this issue, only when I use "rake test" to test things.
As far as I can tell, this is related to when I set up Guard when I was following Michael Hartl's Rails Tutorial, at the end of chapter 3. I followed the directions for setting that up (correctly, as far as I can tell), and this was all in a completely different folder(ultimately my ruby and rails projects do have the same parent folder that they sit in, but are themselves in completely separate ruby_projects and rails_projects folders). If possible, I would like to stop this notification on my ruby application that I am testing. Is there a good way to do this?
Contents of the Rakefile I am using, if that helps:
require "rake/testtask"
Rake::TestTask.new(:test) do |t|
t.libs << "lib"
t.test_files=FileList['test/**/test_*.rb']
end
My test file requires minitest/autorun, and the file for the application that I am testing, then has the normal tests
Seems like there's some weird conflict...
The reason is that Guard::Notifier.connect isn't connected. Normally, when you run guard, Guard.setup is called which does this.
If you're not using guard (e.g. interactively), then calling the following from your Rakefile should work around the problem:
Guard::Notifier.connect(notify: false, silent: true)
Guard::Notifier.disconnect
This will initialize the variable.
For a faster response, always report such issues on the project page on Github. If you can share the project where this occurs, maybe a better fix is possible. (It's best to provide a repository, since it really speeds up fixing things and often errors like this are very hard to simulate without the exact code).

Rails lib class not being loaded in production, works ok in dev

I have a class in the lib directory: lib\db_cache.rb, that defines class DbCache.
My Rails model can access it when in dev mode and also when I run rails console in production mode.
But when I run the production mode rails server, the model class, eg Foo, complains about "uninitialized constant" Foo::DbCache,
org/jruby/RubyModule.java:2677:in `const_missing',
org/jruby/RubyMethod.java:134:in `call'
I have this line in application.rb
config.autoload_paths += %W(#{Rails.root}/lib)
I have also tried the other variations shown in the linked SO questions - but no joy.
I am using jruby 1.7.3 (1.9.3p385) - Java 1.7.0_13-b20, on linux. Rails is version 3.2.12.
I've seen these questions Rails - why would a model inside RAILS_ROOT/lib not be available in production mode? and Best way to load module/class from lib folder in Rails 3? but that doesnt seem to help my case.
Thanks in advance for any ideas on this.
PS My work-hack-around for now is to require 'db_cache' in my model class :(
It sounds like you are trying to extend a class. Without seeing the db_cache.rb file I can't know for sure.
If that is the case it is perfectly fine to have
extend DbCache
in your model class definition
The problem seemed to related to enabling config.threadsafe! in /config/environments/production.rb
This is what I had:
# Enable threaded mode
if defined?(Rails::Server)
puts "Rails Server running - so enable threadsafe!"
config.threadsafe!
end
As I am using jruby, I dont believe this is so much of an issue. At least, when I removed these lines, things worked much better :)

Debugging controller methods by running functional tests

I am in a situation where I need to step through the controller method when I run a functional test. I use ruby-debug to debug through my application. My app is a rails 3.1 app which uses ruby-1.8.7 . I can debug my code by using
rails server --debugger OR
rails console --debugger
I can also stop the code by inserting "debugger" in a model and run it's respective unit test.
But I am not able to do the same thing with the controllers. That is I am not able to stop the code by inserting "debugger" in the methods of the controller and run it's respective functional test.
Has anyone faced this issue before?
Also I use devise for authentication so I need to add the following lines to my test_helper
class ActionController::TestCase
include Devise::TestHelpers
def login_user
#request.env["devise.mapping"] = Devise.mappings[:user]
#user.confirm!
sign_in #user
end
end
Not sure I that is going to effect the debugger in anyway.
I was unable to stop at a debugger inserted in my controller method when I ran the functional tests because I was hitting a before_filter

How to override 'where' in rails 3

I have upgraded my application from rails 2.3.8 to 3.0.3 . But I'm facing a problem. I was using 'find' but the overriding doesn't work in rails 3:
# override activerecord's find to allow us to find by name or id transparently
def self.find(*args)
if args.is_a?(Array) and args.first.is_a?(String) and (args.first.index(/[a-zA-Z\-_]+/) or args.first.to_i.eql?(0) )
find_by_login_slug(args)
else
super
end
end
I'm wondering if there is a way to make this work in rails 3 or even by using where instead.
thanks
The problem you're are facing is an upgrading from a rails 2.3.X to a rails 3.0.X application. Although, it could seem a simple task it isn't, especially if you have a real application and not a toy one. I suggest you to take a look to a screencast series from Rayn Bates, you could start from http://railscasts.com/episodes/225-upgrading-to-rails-3-part-1 to get a complete idea off the problem you are facing.
If you only need to read about ActiveRecord new interface http://m.onkey.org/active-record-query-interface is a great article.

A copy of ApplicationController has been removed from the module tree but is still active

Whenever two concurrent HTTP requests go to my Rails app, the second always returns the following error:
A copy of ApplicationController has been removed from the module tree but is still active!
From there it gives an unhelpful stack trace to the effect of "we went through the standard server stuff, ran your first before_filter on ApplicationController (and I checked; it's just whichever filter runs first)", then offers the following:
/home/matchu/rails/torch/vendor/rails/activesupport/lib/active_support/dependencies.rb:414:in
`load_missing_constant'
/home/matchu/rails/torch/vendor/rails/activesupport/lib/active_support/dependencies.rb:96:in
`const_missing'
which I'm assuming is a generic response and doesn't really say much.
Google seems to tell me that people developing Rails Engines will encounter this, but I don't do that. All I've done is upgrade my Rails app from 2.2 (2.1?) to 2.3.
What are some possible causes for this error, and how can I go about tracking down what's really going on? I know this question is vague, so would any other information be helpful?
More importantly: I tried doing a test run in a "production" environment just now, and the error doesn't seem to persist. Does this only affect development, then, and need I not worry too much?
This is a bug in Rails 2.3.3:
https://rails.lighthouseapp.com/projects/8994/tickets/2948-exception-a-copy-of-actorscontroller-has-been-removed-from-the-module-tree-but-is-still-active
There is a patch for it (but incomplete?) in 2-3-stable:
http://github.com/rails/rails/commit/d37ac7958fc88fdbf37a8948102f6b4e45c530b3
You have a few options to address the problem:
Revert to Rails 2.3.2, wait for 2.3.4 to come out, probably at the end of August. 2.3.3 has a couple bad issues, so that might be best.
The problem should not happen in production mode, nor will it happen in development mode under the Thin server. If you are having this issue on Google Engines in production mode, the patch is your only hope. If it's only in dev mode, you can just run your local server with Thin instead of Mongrel.
If it is Google Engines, you can move off of Google Engines and host your app another way. This seems like a lot of work though.
Best of luck, this is a really bad bug many people are running into.
I addition to the workarounds mentioned in the other answers, I have encountered two others:
Add "config.cache_classes = false" to your config/environments/development.rb file. This has the unfortunate side effect of requiring you to restart your server whenever you want to see your changes.
Add 'unloadable' inside your controller classes in your engine. See http://strd6.com/?p=250 and http://dev.rubyonrails.org/ticket/6001
I haven't tried the second approach, since I found the other solution first, but there is of course a trade-off between avoiding having to edit plugin code, which may be reverted if a newer version of the plugin is downloaded, and then the ease of development provided by not having to restart the development server all the time in the second solution.
i faced with same problem for my new engine on rails 2.3.4 and i found solution here.
calling unloadable method solved my problem.
Weird.
Trying running "rake rails:update" to make sure the configs are scripts are up to date. You may have to check the existing ones against a template application.
i had this error and from memory it was one of one of these three things that fixed it.
1) I needed to update mongrel/rack
2) I had an environment variable from restful authentication that i had moved into the production.rb and development.rb files from the environment.rb - shifting it back to environment.rb seemed to help
3) will_paginate was out of date
We called out to an activerecord model in a namespaced module which overrides the "name" class method. Rails expects that the name method returns Product::Categories::MilkProducts::Firstproduct but gets just Firstproduct and throws an error. So if you get this error first check if you redefined self.name.
Firstproduct.method(:name).owner should be Module
Firstproduct.method(:name).source_location
source:
module Product::Categories::MilkProducts
class Base
def self.name
self.to_s.demodulize
end
end
class Firstproduct < Base
self.product = Product.first
end
end

Resources