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

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

Related

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

Trouble w/ Rubymine and unit tests

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.

How to use omit for Test::Unit::TestCase in Ruby to omit/skip the execution of a certain method/test

I am trying to skip/omit some tests. My environment is Ruby/selenium webdriver/Test Unit.
This how the structure looks:
class HeadTestCase < Test::Unit::TestCase
class UnitTest1 < CCTestCase
class UnitTest1 < CCTestCase
def web_test_01
omit("Skipping this test")
some code related to test
end
Omit method signature is omit("Message",&block)
I dont know what I should put for &block.
Also is this a correct way to skip a test?
Using omit the way you are using it here looks correct. The trick is that you need to be using the test-unit gem and not the minimal version of Test::Unit included with the Ruby distribution.
gem install test-unit
You do not need to use a code block with omit unless you want only part of the code in your test case to be omitted. Use do and end as usual for a block:
def web_test_01
omit("Skipping part of the test") do
# Code here would be skipped
end
# Code here would be executed
end

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?

Using Cucumber With Modular Sinatra Apps

I'm building out a medium-sized application using Sinatra and all was well when I had a single app.rb file and I followed Aslak's guidance up on Github:
https://github.com/cucumber/cucumber/wiki/Sinatra
As the app grew a bit larger and the app.rb file started to bulge, I refactored out a lot of of the bits into "middleware" style modules using Sinatra::Base, mapping things using a rack-up file (config.ru) etc.
The app works nicely - but my specs blew up as there was no more app.rb file for webrat to run against (as defined in the link above).
I've tried to find examples on how to work this - and I think I'm just not used to the internal guts of Cuke just yet as I can't find a single way to have it cover all the apps. I tried just pointing to "config.ru" instead of app.rb - but that doesn't work.
What I ended up doing - which is completely hackish - is to have a separate app.rb file in my support directory, which has all the requires stuff so I can at least test the model stuff. I can also specify routes in there - but that's not at all what I want to do.
So - the question is: how can I get Cucumber to properly work with the modular app approach?
Update to include dealing with multiple Sinatra apps
Require the file where your app comes together and change
def app
Sinatra::Application
end
to
def app
Rack::Builder.new do
map '/a' { run MyAppA }
map '/b' { run MyAppB }
end
end
and just test the app proper.
eg, if you define middleware in your config.ru that you want to test, maybe move loading those into your app's definition.
Thanks to Mr. BaroqueBobcat - the answer now, of course, seems so damn obvious :). Here's the env.rb (/features/support/env.rb):
require 'sinatra'
require 'test/unit'
require 'spec/expectations'
require 'rack/test'
require 'webrat'
require 'app1'
require 'app2'
require 'app3'
Webrat.configure do |config|
config.mode = :rack
end
class MyWorld
require 'test/unit'
set :environment, :test
include Rack::Test::Methods
include Webrat::Methods
include Webrat::Matchers
Webrat::Methods.delegate_to_session :response_code, :response_body, :response
def app
Rack::Builder.new do
map '/' do
run App1 #important - this is the class name
end
map '/app1' do
run App2
end
map '/app2' do
run App3
end
end
end
end
World do
MyWorld.new
end
https://gist.github.com/28d510d9fc25710192bc
def app
eval "Rack::Builder.new {( " + File.read(File.dirname(__FILE__) + '/../config.ru') + "\n )}"
end

Resources