I have a very simple unit test that I copied from http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing.
require_relative "simple_number"
require "test/unit"
class TestSimpleNumber < Test::Unit::TestCase
def test_simple
assert_equal(4, SimpleNumber.new(2).add(2) )
assert_equal(6, SimpleNumber.new(2).multiply(3) )
end
end
Executing this code causes error:
Warning: you should require 'minitest/autorun' instead.
Warning: or add 'gem "minitest"' before 'require "minitest/autorun"'
From:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/test/unit.rb:3:in `<top (required)>'
tc_simple_number.rb:5:in `<main>'
MiniTest::Unit::TestCase is now Minitest::Test. From /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/test/unit/testcase.rb:8:in `<module:Unit>'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/test/unit.rb:670:in `<class:Runner>': undefined method `_run_suite' for class `Test::Unit::Runner' (NameError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/test/unit.rb:255:in `<module:Unit>'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/test/unit.rb:9:in `<module:Test>'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/test/unit.rb:8:in `<top (required)>'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from tc_simple_number.rb:5:in `<main>'
What might be wrong?
Test::Unit::TestCase is deprecated in favor of Minitest::Test. If you want your example to work you need to change a few things:
replace require "test/unit" with require "minitest/autorun"
replace Test::Unit::TestCase with with Minitest::Test
Related
My Ruby file
require 'puppet'
# ...
require 'googleauth'
module PuppetX
module Puppetlabs
module Google_cloud_dns
class Provider < PuppetX::Puppetlabs::Swagger::Provider
def client
self.class.client
end
My RSpec file
I am using this two alternatively, but none of them working:
require_relative'../../lib/puppet_x/puppetlabs/google_cloud_dns/provider'
File.expand_path("../../lib/puppet_x/puppetlabs/google_cloud_dns/provider.rb",File.dirname(__FILE__)))
require 'spec_helper'
require_relative '../../lib/puppet_x/puppetlabs/google_cloud_dns/provider'
File.expand_path("../../lib/puppet_x/puppetlabs/google_cloud_dns/provider.rb", File.dirname(__FILE__))
describe Provider do
it "checks function" do
provide = Provider.new
provide.should respond_to :client
end
end
The error which I am getting
rspec Mymodule/gc_dns/puppet-google_cloud_dns/spec/classes/provider_spec.rb --format documentation/home/vivekkumarmishra17/Mymodule/gc_dns/puppet-google_cloud_dns/spec/classes/provider_spec.rb:19:in `<top (required)>': uninitialized constant Provider (NameError)
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1361:in `load'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1361:in `block in load_spec_files'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `each'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `load_spec_files'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:106:in `setup'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:92:in `run'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:78:in `run'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:45:in `invoke'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/exe/rspec:4:in `<top (required)>'
from /usr/local/bin/rspec:23:in `load'
from /usr/local/bin/rspec:23:in `<main>'
Can anyone tell what I am doing wrong?
When running rspec to test a feature test using Sinatra Base, we are getting the following error.
This is how our feature tests looks
require 'capybara/rspec'
feature 'Enter names' do
scenario 'submitting names' do
visit('/')
fill_in :player_1_name, with: 'Dave'
fill_in :player_2_name, with: 'Mittens'
click_button 'Submit'
expect(page).to have_content 'Dave vs. Mittens'
end
end
This is our Gemfile:
source 'https://rubygems.org'
gem 'sinatra'
gem 'rspec-sinatra'
gem 'capybara'
error:
1.1) Failure/Error: visit "/"
ArgumentError:
rack-test requires a rack application, but none was given
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/rack_test/driver.rb:16:in `initialize'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:372:in `new'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:372:in `block in <top (required)>'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/session.rb:79:in `call'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/session.rb:79:in `driver'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/session.rb:227:in `visit'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
# ./spec/feature/feature_spec.rb:5:in `block (2 levels) in <top (required)>'
1.2) Failure/Error: Unable to find matching line from backtrace
ArgumentError:
rack-test requires a rack application, but none was given
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/rack_test/driver.rb:16:in `initialize'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:372:in `new'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:372:in `block in <top (required)>'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/session.rb:79:in `call'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/session.rb:79:in `driver'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/session.rb:103:in `reset!'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:257:in `block in reset_sessions!'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:257:in `each'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:257:in `reset_sessions!'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/rspec.rb:20:in `block (2 levels) in <top (required)>'
We've tried requiring the spec_helper file in our feature test, and also requiring our app.rb in the feature test and we get a different error saying it cannot load sinatra-base
Here is our spec_helper
ENV['RACK_ENV'] = 'test'
require File.join(File.dirname(__FILE__), '..', 'app.rb')
require 'capybara'
require 'capybara/rspec'
require 'rspec'
Capybara.app = Battle
RSpec.configure do |config|
config.include Capybara::DSL
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
Any direction would be massively appreciated.
Thanks
Russ
Have a look here:
http://recipes.sinatrarb.com/p/testing/rspec
It suggests, among other things, adding the following to your spec_helper.rb:
module RSpecMixin
include Rack::Test::Methods
def app() Sinatra::Application end
end
RSpec.configure do |config|
config.include RSpecMixin
end
I am trying to create a migration for a Ruby project using Sinatra. My model class is inside the app.rb file. When I run create_migration:
rake db:create_migration NAME=create_admins
I get the following exception with trace:
rake db:create_migration NAME=create_admins --trace
rake aborted!
Don't know how to build task 'db:create_migration'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task_manager.rb:62:in `[]'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:149:in `invoke_task'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `each'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.2.1/bin/rake:23:in `load'
/usr/local/rvm/gems/ruby-2.2.1/bin/rake:23:in `<main>'
/usr/local/rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'
My app.rb is copied below:
require 'rubygems'
gem 'activerecord'
require 'sqlite3'
require 'active_record'
ActiveRecord::Base.logger = Logger.new(File.open('database.log', 'w'))
#ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:host => "localhost",
:database => 'test6.db'
)
class Bill < ActiveRecord::Base
belongs_to :admin
end
class Admin < ActiveRecord::Base
validates :email, presence: true, uniqueness: true
validates :name, presence: true
has_many :bills
end
My Rakefile is copied below:
# Rakefile
require './app'
require 'sinatra'
require 'active_record'
#require 'rake'
Note: I am not using rails - I am using sinatra with activerecord
You can use sinatra-activerecord instead 'activerecord', and make minor changes like below -
in Gemfile
source 'https://rubygems.org'
gem "sinatra"
gem "pg"
gem "activerecord"
gem "sinatra-activerecord"
in rakefile.rb
require "./app"
require "sinatra/activerecord/rake"
This should work
I want to extend the faker gem for rails to also generate other random date (in my case computer game names)
#lib/extended_faker.rb
require 'faker'
require 'extended_faker/game'
#lib/extended_faker/game.rb
Module Faker
class Game < Faker::Base
class << self
def name
fetch('game.name')
end
end
end
end
#config/locals/faker_en.yml
en:
faker:
game:
name: ["a", "b", "c"]
#config/application.rb
...
config.autoload_paths += Dir["#{config.root}/lib/**/"]
...
then when i run it in a rails console i get the following
Loading development environment (Rails 3.2.9)
1.9.3p194 :001 > Faker::Game.name
LoadError: Expected /home/enermis/School/Projects/IG/test/lib/extended_faker/game.rb to define Game
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:503:in `load_missing_constant'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:192:in `block in const_missing'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `each'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `const_missing'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:514:in `load_missing_constant'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:192:in `block in const_missing'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `each'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `const_missing'
from (irb):1
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
When i change the lib/extended_faker/game.rb file to this
require 'faker'
require 'extended_faker/game'
include 'extended_faker/item'
include 'extended_faker/team'
i get weird behavior in the console
Loading development environment (Rails 3.2.9)
1.9.3p194 :001 > Faker::Game.name
=> "b"
1.9.3p194 :002 > Faker::Game.name
NameError: uninitialized constant Faker::Game
from (irb):2
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
The weird thing i don't understand is that the first time i run the generator, i get a valid result, but the second time around i get an error...
What am i missing?
you shoud put it in
lib/faker/game.rb
starting in the lib directory, rails convention is outermost module name -> in are folder names. Then the actual class/module name is the file name, underscored to the camelcase.
Another example
module Foo
module Bar
class BazParty
def self.hello
puts "hello"
end
end
end
end
would go in lib/foo/bar/baz_party.rb
$ rails generate rspec:install
/Users/AlbertoRDZ/Desktop/rails_projects/sample_app/config/application.rb:7:in `<top (required)>': undefined method `groups' for Rails:Module (NoMethodError)
from /Users/AlbertoRDZ/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.9/lib/rails/commands.rb:15:in `require'
from /Users/AlbertoRDZ/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.9/lib/rails/commands.rb:15:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Maybe it works with the following:
In config/application.rb substitute
if defined?(Bundler)
Bundler.require(*Rails.groups(:assets => %w(development test)))
end
with
Bundler.require(:default, Rails.env) if defined?(Bundler)