Trouble w/ Rubymine and unit tests - ruby

DISCLAIMER: I am very new to ruby, still trying to get my feet wet, so this could be hugely stupid issue.
I'm trying to get a very simple project and one unit test working, and the Universe is throwing a LifeException (I just can't figure this out)
Using Rubymine 4.0.1 on Mac OS X, 10.7.3.
Launched RubyMine and created a new project (not Rails) "TestExample"
Created a new Ruby Class, file is my_class.rb.
class MyClass
def say_hi
puts "Hi!"
end
end
my = MyClass.new
my.say_hi
Create a new TestUnit Test Template, file is "my_test.rb"
require "test/unit"
class MyTest < Test::Unit::TestCase
def test_create
#my = MyClass.new
end
end
At this point I have two issues:
1. How do I 'require' my class in my tests? If I change the above test case to:
require "test/unit"
require "my_class"
class MyTest < Test::Unit::TestCase
def test_create
#my = MyClass.new
end
end
and attempt to run my "All tests in: TestExample" configuration, I get a "Exception message: cannot load such a file -- my_class". The Tests folder and working directory are pointed to the location of the files. (every file is in the same folder)
The other is a "Unable to attach test reporter to test framework".
I've googled and attempted to figure this out to no avail. I realize this is two questions in one, and if I could just get tests working, I'd be happy.
Thanks for any input and don't laugh to hard at my uber-ruby-noobness.
[Update] - This only happens when using the RVM: ruby-1-9.3-p125 SDK. If I use the ruby-1.8.7-p249(/usr/bin/ruby), it does work. This has to be some configuration issue.

For the first question, try:
require_relative 'my_class'
I can't answer the second.

Related

How do I implement a custom buildr task defined externally?

I have a task which I'm trying to refactor into an external module so that I can later separate it from this project and use it for other projects.
When I try to run my task, I get an error:
Buildr aborted!
NoMethodError : undefined method `path_to' for nil:NilClass
Essentially it seems like the code for the project.task block is never called.
The code is as follows. Some of the code comes from the working code for the compile task, so I know that those bits are probably correct. Other parts come from documented examples for buildr, which from experience earlier today can be taken with a grain (or sometimes an entire lake) of salt. I can't figure out what I've done wrong, though.
module MacAppBundle
include Buildr::Extension
class MacAppBundleTask < Rake::Task
attr_accessor :app_name
def initialize(*args)
super
enhance do |task|
#TODO: #project is always nil here because associate_with is never called
app = #project.path_to("target/#{app_name}.app")
if File.exists?(app)
FileUtils.rm_rf(app)
end
Dir.mkdir(app)
#... omitting copying the rest of the stuff into the bundle ...
end
end
protected
def associate_with(project)
#project = project
end
end
before_define do |project|
mac_app_bundle = MacAppBundleTask.define_task('mac_app_bundle')
project.task 'mac_app_bundle' do |task|
#TODO: This code never executes. Why?
mac_app_bundle.send :associate_with, project
project.local_task('mac_app_bundle')
end
end
after_define do |project|
#TODO: This bit is definitely questionable because I can't find any documentation
# or working examples of similar code.
task('mac_app_bundle' => project.package(:jar))
end
def mac_app_bundle
task('mac_app_bundle')
end
end
class Buildr::Project
include MacAppBundle
end
#TODO: Find a place to move this. Seems weird to have to call it in global scope.
Project.local_task('mac_app_bundle') do |name|
puts "Creating Mac OS X app bundle for #{name}"
end
The correct way appears to be:
before_define do |project|
mac_app_bundle = MacAppBundleTask.define_task('mac_app_bundle')
mac_app_bundle.send :associate_with, project
project.task('mac_app_bundle')
end

Adding a generator for my custom gem (Rails > 3)

I'm trying to write a Ruby gem (with Rails 3.2.13), I have created a generator to do a file copy.
Following is my generator code
#<mygemname>/lib/generators/mygemname/mygemname_generator.rb
require 'rails/generators'
require 'rails/generators/migration'
module Mygemname
module Generators
class ConfigGenerator < Rails::Generators::Base
p "testing generator"
end
end
end
then I go to the test app in my <mygemname>/spec/dummy (I use Rspec for testing), so from my dummy app, when I run
rails g I get
Mygemname:
mygemname:config
but when I run rails g endless:config, I get
Could not find generator endless:config.
but when I run rails g endless, I get the correct result. But I would like to have the command as rails g endless:config, how can I do that?
Try renaming the file: /lib/generators/mygemname/config_generator.rb
And structure config_generator.rb a bit differently:
class Mygemname
class ConfigGenerator < Rails::Generators::Base
p "testing generator"
end
end

How to use RSpec to test a Sinatra application within a gem?

I am writing a gem which includes a Sinatra application that a developer can extend. For example:
# gem code:
require 'sinatra'
module Mygem
class Application < Sinatra::Base
get 'auth/login' {}
get 'auth/logout {}
end
end
# developer code:
require 'mygem'
class DeveloperApp < Mygem::Application
# ..
end
I am also getting started using RSpec. How should I configure RSpec for testing this functionality?
The references above are all informative and useful but mostly rails specific. I found it quite hard to find a simple recipe for a basic test of a modular Sinatra app, so I am hoping this will answer the question for others. Here is a completely bare-bones, small as possible test. This is probably not the only way to do it, but it works well for a modular app:
require 'sinatra'
class Foo < Sinatra::Base
get '/' do
"Hello"
end
end
require 'rack/test'
describe Foo do
include Rack::Test::Methods
def app
Foo.new
end
it "should be testable" do
get '/'
last_response.should be_ok
end
end
Note that there is no need to have the server running when you launch the test (some tutorials I saw implied that you do) - it's not an integration test.
It's actually pretty simple -- just add rspec to your gemfile (then bundle install), and make a directory in your gem called spec/. Once you've done that, add a file spec/spec_helper.rb that contains some configuration for rspec (mostly requiring various files from your library) as well as defining some helper methods for your specs. Then, for each model and controller, make a file called my_model_name_spec.rb or my_controller_name_spec.rb, and do the test there.
Here are some useful resources for getting started with rspec:
Railscasts:
http://railscasts.com/episodes/275-how-i-test
http://railscasts.com/episodes/71-testing-controllers-with-rspec
http://railscasts.com/episodes/157-rspec-matchers-macros/
And for some more advanced (but well-explained) stuff:
http://benscheirman.com/2011/05/dry-up-your-rspec-files-with-subject-let-blocks
Be sure to include the rack-test gem.
You spec helper should have:
require 'rack/test'
require 'foo' # or where ever your app is
# This can go in a helper somewhere
module AppHelper
def app
Foo.new
end
end
RSpec.configure do |config|
config.include Rack::Test::Methods
config.include AppHelper
end
Then, your spec can be as follows:
require 'spec_helper'
# Example app. Delete this example.
class Foo < Sinatra::Base
get '/' do
'Jesse Pinkman'
end
end
describe Foo do
it 'is testable' do
get '/' do
expect(last_response).to be_ok
end
end
end

Authlogic with Capybara + Cucumber + Selenium Driver not working

This is the error I get when I run a cucumber test with #javascript with authlogic:
You must activate the Authlogic::Session::Base.controller with a controller object before creating objects
This is my authlogic support code in feature/support/authlogic.rb:
require "authlogic"
require "authlogic/test_case"
World(Authlogic::TestCase)
ApplicationController.skip_before_filter :activate_authlogic
Before do
activate_authlogic
end
This is how I created a session:
def create_session
Session.create(:name => "test", :password => "test-33")
end
Without #javascript, it will not give me the error about authlogic not being activated, but with #javascript it does. How do I fix this problem?
Selenium and capybara-webkit use separate threads when launching processes. When you run activate_authlogic it does the following
Authlogic::Session::Base.controller = (#request && Authlogic::TestCase::RailsRequestAdapter.new(#request)) || controller
This winds up setting a thread local variable for :authlogic_controller. The problem is that this gets lost when you start using new threads in scenarios tagged with #javascript.
For me, the fix was to monkeypatch authlogic code like so
module Authlogic
module Session
module Activation
module ClassMethods
def controller
if !Thread.current[:authlogic_controller]
Thread.current[:authlogic_controller] = Authlogic::TestCase::MockController.new
end
Thread.current[:authlogic_controller]
end
end
end
end
end
This replicates what is done in acivate_authlogic. Make sure you only patch your test environment.

Test::Unit::TestCase from rakefile?

I'm stuck trying to call individual unit test methods from a rake file to automate my testing but I keep getting an error. Every time I run 'rake manage' I get an error in my 'manage' task saying: wrong number of arguments 0 for 1. Here is my rake file:
require "test_file"
task :default => [:commands]
task :manage do
myTest = Unit_Test.new
myTest.test
end
And my actual class that has uses the Test::Unit::TestCase class. This is in a separate file called 'test_file.rb'.
class Unit_Test < Test::Unit::TestCase
include Rack::Test::Methods
def test
puts "this is just a test"
end
end
There error is pointing to:
myTest = Unit_Test.new
How do I call individual methods from this class? I basically want to call certain methods from this class in different tasks but I cannot get it to work. How do I get this working?
Have you considered doing
ruby test_file.rb --name test_method_to_be_run
Or do you need to run multiple test methods?
Also, what version of Ruby are you using? 1.8 or 1.9?

Resources