Trying to write a simple unit test using shoulda and rails 3.
test/unit/user_test.rb
class UserTest < Test::Unit::TestCase
should validate_presence_of(:password, :on => :create)
should validate_presence_of(:handle, :email)
should validate_confirmation_of(:password)
should validate_length_of(:handle, :within => 6..15)
should validate_uniqueness_of(:handle)
should validate_format_of(:handle, :with => /\A\w+\z/i)
should validate_length_of(:email, :within => 6..100)
end
Relevant parts of Gemfile
group :test do
gem 'shoulda'
gem 'rspec-rails', '2.0.0.beta.12'
end
When I try to run this using rake test --trace I receive the following error:
** Execute test:units
/Users/removed/removed/removed/app_name/test/unit/user_test.rb:5: superclass mismatch for class UserTest (TypeError)
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:227:in `load_dependency'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:9
from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:9:in `each'
from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:9
from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:5:in `each'
from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:5
I understand the error, I just don't get where another UserTest class would be defined that's giving me this issue. Any thoughts?
Mike
Check the output of find . | xargs grep -l UserTest against accidental duplicated uses of the class name.
The only way I could imagine avoiding this mistake is by doing the following:
UserTest = Class.new(Test::Unit::TestCase)
class UserTest # Or class UserTest < Test::Unit::TestCase is also allowed
should validate_presence_of(:password, :on => :create)
should validate_presence_of(:handle, :email)
should validate_confirmation_of(:password)
should validate_length_of(:handle, :within => 6..15)
should validate_uniqueness_of(:handle)
should validate_format_of(:handle, :with => /\A\w+\z/i)
should validate_length_of(:email, :within => 6..100)
end
if you were to repeat
UserTest = Class.new(Test::Unit::TestCase) # repeated
you'd get
warning: already initialized constant UserTest
But this approach would look a little weird.
Related
Note: I am a Ruby and programming novice.
I have a class called JourneyLog I am trying to get a method called start to instantiate a new instance of another class, called Journey
class JourneyLog
attr_reader :journey_class
def initialize(journey_class: Journey)
#journey_class = journey_class
#journeys = []
end
def start(station)
journey_class.new(entry_station: station)
end
end
When I go into irbi get the following issue
2.2.3 :001 > require './lib/journeylog'
=> true
2.2.3 :002 > journeylog = JourneyLog.new
NameError: uninitialized constant JourneyLog::Journey
from /Users/BartJudge/Desktop/Makers_2018/oystercard-challenge/lib/journeylog.rb:4:in `initialize'
from (irb):2:in `new'
from (irb):2
from /Users/BartJudge/.rvm/rubies/ruby-2.2.3/bin/irb:15:in `<main>'
2.2.3 :003 >
I also have the following Rspec test
require 'journeylog'
describe JourneyLog do
let(:journey) { double :journey, entry_station: nil, complete?: false, fare: 1}
let(:station) { double :station }
let(:journey_class) { double :journey_class, new: journey }
describe '#start' do
it 'starts a journey' do
expect(journey_class).to receive(:new).with(entry_station: station)
subject.start(station)
end
end
end
I get the following Rspec failure;
1) JourneyLog#start starts a journey
Failure/Error: expect(journey_class).to receive(:new).with(entry_station: station)
(Double :journey_class).new({:entry_station=>#<Double :station>})
expected: 1 time with arguments: ({:entry_station=>#<Double :station>})
received: 0 times
# ./spec/jorneylog_spec.rb:9:in `block (3 levels) in <top (required)>'
I am at a total loss on what the problem is, or where to look for some answers.
I'm assuming I'm not injecting the Journey class properly, but thats as far as I can get myself.
Could someone provide some assistance?
In the journeylog.rb file you need to load the Journey class:
require 'journey' # I guess the Journey class is defined in lib/journey.rb
In the spec file you need to pass journey_class to the JourneyLog constructor:
describe JourneyLog do
subject { described_class.new(journey_class: journey_class) }
# ...
I have have started with simple example of Rspec,Capybara. I have come across few issues. This issues are probably because I have experience with cucumber and page_object gem, but here I am using capybara and Site_prism gem.
I have tried:
my_example_spec.rb
require_relative 'Support/spec_helper'
require_relative 'pages/login_page'
describe 'My behaviour' do
it 'should do something'do
#login_page = LoginPage.new
#login_page.load
#login_page.login('autouser','password')
end
end
and login_page.rb
class LoginPage < SitePrism::Page
set_url "/login"
element :username, "input[id='username']"
element :password, "input[id='password']"
element :submit, "input[id='submit']"
def login(username,password)
#login_page.username.set username
#login_page.password.set password
#login_page.submit.click
end
end
Issues are:
When I run my_example_spec.rb it gives error
Testing started at ...
Run options: include {:full_description=>/My\ behaviour\ should\ do\ something/}
NoMethodError: undefined method `username' for nil:NilClass
./pages/login_page.rb:10:in `login'
./my_example_spec.rb:11:in `block (2 levels) in <top (required)>'
-e:1:in `load'
-e:1:in `<main>'
Shouldn't it be on(LoginPage).login (autouser, password). It should navigate to the page and run login method. It is how it works in page_object gem whats the equivalent of site_prism gem
The login method in your LoginPage class should be
def login(username,password)
username.set username
password.set password
submit.click
end
#login_page is not an instance variable of the LoginPage class so it is not accessible inside the class. It's also not necessary inside the class since you're already inside the class.
I'm trying to write my first rspec test for a simple puppet class. Here's the class, rspec test and results. I'm new to rspec and would like to know what I'm doing wrong here. I follow the directions here http://rspec-puppet.com/setup/ to configure rspec-puppet for these tests. Thanks.
Class example for cron module init.pp
class cron {
service { 'crond' :
ensure => running,
enable => true
}
}
Rspec Test
require '/etc/puppetlabs/puppet/modules/cron/spec/spec_helper'
describe 'cron', :type => :module do
it { should contain_class('cron') }
it do should contain_service('crond').with(
'ensure' => 'running',
'enable' => 'true'
) end
end
Results
FF
Failures:
1) cron
Failure/Error: it { should contain_class('cron') }
NoMethodError:
undefined method `contain_class' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000001c66d70>
# ./cron_spec.rb:5:in `block (2 levels) in <top (required)>'
2) cron
Failure/Error: it do should contain_service('crond').with(
NoMethodError:
undefined method `contain_service' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000001c867b0>
# ./cron_spec.rb:6:in `block (2 levels) in <top (required)>'
Finished in 0.00237 seconds
2 examples, 2 failures
Failed examples:
rspec ./cron_spec.rb:5 # cron
rspec ./cron_spec.rb:6 # cron
Where did you pick up the
describe 'cron', :type => :module
syntax? That may be obsolete.
With current versions of rspec-puppet, you describe
classes
defined types
functions
hosts
You basically just want to put your spec right into spec/classes/cron_spec.rb, that should do half your work for you, e.g.
# spec/classes/cron.rb
require "#{File.join(File.dirname(__FILE__),'..','spec_helper.rb')}"
describe 'cron' do
it { should contain_service('crond').with('ensure' => 'running') }
it { should contain_service('crond').with('enable' => 'true') }
end
It is good practice to have distinct tests for each attribute value, so that possible future regressions can be identified more accurately.
Do see the README.
For a nice example of a well structured module test-suite see example42's modules.
i'm having problems understanding how to work with Rack::Test, the issue i have is with POST. This are the classes and the error:
hellotesting.rb
require 'sinatra'
post '/foo' do
"Hello #{params[:name]}."
end
This is the test:
require 'hellotesting'
require 'test/unit'
require 'rack/test'
set :environment, :test
class HelloWorldTest < Test::Unit::TestCase
def test_it_says_hello_to_you
browser = Rack::Test::Session.new(Rack::MockSession.new(Sinatra::Application))
post "/foo", "name" => "Bryan"
assert browser.last_response.ok?
assert_equal 'Hello Bryan', browser.last_response.body
end
end
And the output:
1) Error:
test_it_says_hello_to_you(HelloWorldTest):
ArgumentError: wrong number of arguments (1 for 0)
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1141:in `name'
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1141:in `send'
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1141:in `compile!'
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1141:in `each_pair'
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1141:in `compile!'
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1129:in `route'
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1118:in `post'
(__DELEGATE__):3:in `send'
(__DELEGATE__):3:in `post'
testingjeison.rb:11:in `test_it_says_hello_to_you'
It may be that you need to include the Rack::Test mixins into your individual classes. I mainly use RSpec, which doesn't use classes, but does use a specialized variant of Ruby's include for pulling in extra functionality. You may want to try putting in include Rack::Test::Methods inside your HelloWorldTest case class definition. Sinatra's testing has more information for testing with Rack::Test.
I've got a problem with this class
require "test/unit"
require "selenium/client"
class Test < Test::Unit::TestCase
def setup
#verification_errors = []
#selenium = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*chrome",
:url => "http://change-this-to-the-site-you-are-testing/",
:timeout_in_second => 60
#selenium.start_new_browser_session
end
def teardown
#selenium.close_current_browser_session
assert_equal [], #verification_errors
end
def test_test
#selenium.open "/apj/gestionnaire/flux.ex"
#selenium.wait_for_pop_up "_self", "30000"
end
end
it says to me that it's not a class :
/test.rb:4: Test is not a class (TypeError)
from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
from C:/Documents and Settings/Micro/My Documents/Aptana RadRails Workspace/.metadata/.plugins/org.rubypeople.rdt.testunit/ruby/RemoteTestRunner.rb:301
anyone have any idea ?
Regards
Bussiere
Using Test as your class name is a bad idea. It's an existing constant (referring to a module) as soon you require test/unit
require "test/unit"
Test.class # => Module
Use a different name for your test case.
Using Test as your class name is a bad idea.
Wrong ! Today a new version of the rspec-rails gem has been released fixing this issue in some cases.
You can take a look at the the changelog file:
Fix "Test is not a class (TypeError)" error when using a custom Test class in Rails 4.1 and 4.2. (Aaron Kromer, #1295)