Test Unit fails to load xsd file - ruby-on-rails

I have a ruby file that requires a file which has require 'xsd/qname'
and all my files work fine, but when I go to test them with test unit I keep getting these errors
LoadError: no such file to load -- xsd/qname
I've been scouring google for a while and fail to see a solution. (I'm new to test unit so it might be incredibly simple).
EDIT
I believe my problem is related to the fact that the code is in a gem and not the rails environment, therefore the code using it loads rails while these tests do not.
The odd part is if I go in the gem with irb, I can require 'xsd/qname', but I can't require 'soap/rpc/driver' which is another error I was getting

It's probably path related. It's difficult to advise a best solution without seeing how your project is laid out, but try replacing the require statement with:
require File.dirname(Rails.root + '/xsd/qname')
You may need to adjust that depending on where that file exists within your project.

I think the problem was using files built in ruby 1.8 with 1.9. Using soap4r solved my problem.
https://github.com/spox/soap4r-spox
Fyi if you just want to just rescue the error do this:
begin
require 'xsd/qname'
rescue LoadError
puts "xsd/qname not found...."
exit
end

Related

Loading Rails project's app paths in Rspec

I'm writing an application in Rails 5 and using Rspec for tests. I'm having a lot of trouble with require 'some_model' or require 'some_controller' in my spec files. Every time I run a spec, it acts lost and doesn't seem to understand that it's in a Rails app. The suggestions I've seen range from
config.autolad_paths += %W( #{config.root}/app )
to
$LOAD_PATH << '~/my_app/app/models' which sort of works, except that my tests throw an unitialized constant for ApplicationRecord
So for all the digging around I've done, I still haven't found a good way require my MVC files while keeping my specs and spec_helper clean. There doesn't seem to be any defacto way to do so, so how do you all suggest doing it?
can you not take on the entire stress of RSpec configuration. Use the rspec-rails gem and look through the documentation to use it within your app. I guess you can get a base config with rails generate rspec:install after including the gem in your project.

Ruby on Rails - using modules from a different directory

This is a very basic question, but I can't find exactly the answer I need.
I have the following code in trunk/app/models/parsers/my_file.rb in my dev environment:
def initialize
...
#logger = Utils::SingletonLogger.get_logger
#logger.debug("Instantiating my_file object")
end
It runs fine.
But when I deploy it to the test environment (all code should be identical, but I'm not sure where to start looking for differences if there are any), Rails complains that it can't find Parsers::MyFile::Utils. The Utils module I want to use is in app/lib/my_utils.rb. This makes me think that Rails is creating some sort of namespace for the code in the parsers sub-directory, and only looking there for the Utils module, but I haven't been able to figure out how to make it work for me. Is there some main, application level prefix I can use to specify to look outside of the current directory structure?
I've tried adding require 'my_utils' and require_relative '../../../lib/my_utils.rb'. The former can't find the file, the latter just throws the same error as when I don't have any require at all. I'm not sure if I should have to require this or not.
In any case, I clearly don't quite understand how to refer to code in modules in a different directory, I don't understand when/why rails needs an explicit path at some times/environments but not others, and I don't know how to make Rails look outside of the current file for code. Any help with any of these would be appreciated.
Oh, I'm using Ruby 1.9.3, and rails 3.2.1.
Edit: It just started working, without any changes to the application.rb or environment files. It doesn't even have a require in the current version. Is there any obvious reason for it not to work at first, then to work after another server restart? (I'm pretty sure I restarted it after the code went in - I don't think I just forgot to do that before.)
Anyway, thanks for your help - I really do appreciate it.
You can manually add directories you want to include in application.rb. Might want to make sure in your application.rb or test.rb config files you have this autoload_paths in there, yours might be specific to your development.rb file.
config.autoload_paths += %W(#{config.root}/lib/)

SQLite3::CantOpenException: unable to open database file while testing Ruby Gem

I'm running into this error message while testing a gem on travis:
SQLite3::CantOpenException: unable to open database file
I'm not even hitting the database in any of the tests, which is weird. Looks like the backtrace points a finger at fixtures, though I don't have any fixtures. Even if I did, the db is sqlite3...shouldn't it be able to be created on the fly?
You can see the build output here: http://travis-lite.com/schneems/maildown/jobs/9662299
Any help is appreciated.
Your test_helper.rb file includes this
# Load fixtures from the engine
if ActiveSupport::TestCase.method_defined?(:fixture_path=)
ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
end
Since you don't have fixtures, I'm guessing this is what's causing the trouble. You probably want to remove those lines?
You also don't have a /db folder in your project, try adding that to see if it fixes the build.

How to make Rubymine working with minitest framework

Can't configure Rubymine to work with minitest normally. All the time it throws me exceptions
Unable to attach test reporter to test framework or test framework quit unexpectedly
Also, I can't run test by one (only all test running working), because in that case context do not load and all my classes goes to be undetermined constants (NameError: uninitialized constant <MyVariableType>).
I'm currently working with RubyMine 5 via Windows 7. (Ruby 1.9.3).
If someone know how to configure it properly i'll be very appreciate for your help.
Ok, great! I finally solve all my problems and now my rubymine working with minitest.
The short instruction:
Read this manual and do all the stuff step by step very careful
If it helps then say 'Yohuu!!!' and dancing victorious jig, if it
still not working correctly goes to step 3
Add 'test-unit' gem to your gem file and update it with bundler.
When creating test do not forget to add require 'test_helper' add
the top of the file.
IMPORTANT: your test method names should start with 'test_' pattern, like test_my_supercool_method
UPD 1: If you use Ruby 2.0, you don't need to use win32Console gem on Windows platform, even if jetbrains doc say that you should.
If you are following this guide (https://www.jetbrains.com/help/ruby/2016.1/minitest.html?origin=old_help), you don't need to include minitest-reporters in your Gemfile or have this:
require 'minitest/reporters'
MiniTest::Reporters.use!
in your test_helper.rb file if you are using MiniTest 5 or newer. I was following a tutorial that was a little outdated and ran into this issue. Be sure to delete your .idea directory and restart RubyMine and you should be good to go!
I just went through this with the undefined method 'format_backtrace' error, and was getting green passing when the tests were failing and crashing.
Read the notes closely, if you are running minitest > 5.0, you don't need the minitest-reporters gem.
If you include it, perhaps like I did following the instructions, then you get the errors, and errors are not reported- (in my case, anyways). So back out and remove the require minitest/reporters from your tests, and the MiniTest::Reporters.use! line and things should be good to go.

rubymine only able to run model tests one by one

I am able to right-click on any of my 3 spec/models but when I right click the spec/models folder and select 'run all tests in models' I get 'Unable to attach test reporter to test framework'.
The first line of my model tests is: require 'spec_helper.rb'
I posted this answer for another question. It may help with this one as well:
On my system, the problem was the "redgreen" gem. It automatically colors the progress marks (dots) and the Test::Unit summary messages based on success or failure. There must be something in RubyMine that's trying to parse the Test::Unit results (the output of "rake test", I imagine), and it's choking on the ANSI sequences.
I commented out "require 'redgreen'" in my test/test_helper.rb, and the problem went away. I really like redgreen for executing "rake test" from the shell, though, so I put this in test_helper to make it work for both "rake test" and within RubyMine:
require 'redgreen' if $stdin.tty?
It may not be redgreen that's causing your problem, but be suspicious of anything which might create unconventional Test::Unit output.
Good luck!
I had the same issue and had to add the following to /etc/launcd.conf (I had to create this file as well):
setenv DYLD_LIBRARY_PATH /usr/local/mysql/lib/
and reboot. There are other ways to do it as well (a plist file, adding this environmental variable to RubyMine, etc… but this was most reliable. Of course, this assumes that you use MySQL.
The answer in the end for my setup was to fiddle around with the IDE settings including the ruby version, making sure it was 1.9.2 and the directories referenced in the config. screens were correct. This plus some restarts resolved the issue.

Resources