NoMethodError: undefined method `assert_true'
Am getting above error while running tests using test-unit in ruby. test-unit gem and rake versions are below,
test-unit (2.5.5)
rake (10.1.0)
Sample test file:-
require 'test/unit'
class Sample < Test::Unit::TestCase
def setup
# code block
end
def test_sample
assert_true("test"=="test")
end
def teardown
# code block
end
end
How to solve this ?
I solved the problem by using following way. No need to change assert statements.
require 'rubygems'
gem 'test-unit'
require 'test/unit'
class Sample < Test::Unit::TestCase
def setup
# code block
end
def test_sample
assert_true("test"=="test")
end
def teardown
# code block
end
end
assert_true does not appear to be in the list of available assertions for test-unit. Try using assert. Reference: http://ruby-doc.org/stdlib-2.0.0/libdoc/test/unit/rdoc/Test/Unit/Assertions.html
Since 1.9.2, test/unit is a wrapper around minitest, implemented directly in the ruby source code. The assert_true method does not exist in the new implementation, just use assert instead, as Simon Brahan already suggested. So the gem source you were looking at is no longer in use. The now relevant documentation is here.
Related
Working on a Ruby gem and trying to use FactoryBot inside with RSpec.
I have this in support/factory_bot.rb:
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
config.before(:suite) do
FactoryBot.find_definitions
end
end
and in spec_helper.rb:
require 'support/factory_bot'
When I try to run the spec rake task, I get this error:
support/factory_bot.rb:2:in `block in <top (required)>': uninitialized constant FactoryBot (NameError)
What am I missing? This used to work fine when I was using the old factory_girl gem, but it has broken with the rename to factory_bot. Thanks!!
Doh. Silly mistake here, running bundle exec rake spec instead of rake spec solved it.
Also had to add require 'factory_bot' to the top of support/factory_bot.rb
Overview just in case you are doing this from scratch
installation rspec details here (basically add gem to Gemfile then run bundle install)
initialize RSPEC in your rails project rails g rspec:install
create new file your spec/support/factory_bot.rb add the following base code:
require 'factory_bot'
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
# RSpec without Rails
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
config.before(:suite) do
FactoryBot.find_definitions
end
end
add reference on spec/rails_helper.rb
require 'support/factory_bot'
as well as remove any fixture unused reference like this one
config.use_transactional_fixtures = true
That should be it!, finally run any spec file you want inside rspec default folders
e.g.:
spec/features/my_action_spec.rb
spec/models/my_model_spec.rb
spec/task/my_task_spec.rb
or run them all and check depending on your setup
rspec
rails rspec
bundle exec rspec
hope this helps someone with the whole RSPEC + FactoryBot Gem installation process
I had this problem too, remove the
require 'support/factory_bot'
There's a line on rails_helper, just uncomment it:
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
I had encountered and issue similar to this. I worked around it by removing the default testing suite ( MiniTest ). When you create a rails application and intend on using rspec and factory_bot, use the code below in the command line:
rails new myapp -T
Hope this helps xP
In my case I had to put those lines below 'require "rspec/rails" in file:
spec/rails_helper.rb:
like:
require "rspec/rails"
require_relative "support/factory_bot"
require_relative "support/chrome"
I'm trying to set up a basic framework using Capybara, Cucumber and SitePrism, but I keep getting the error, "uninitialized constant SitePrism (NameError)" when I kick off a test.
Gemfile:
gem 'cucumber', '2.3.3'
gem 'capybara', '2.6.2'
gem 'selenium-webdriver', '2.53.0'
gem 'rspec'
gem 'site_prism'
gem 'mime-types', '>2.6', '<2.99.1'
Env.rb
require 'capybara'
require 'capybara/rspec'
require 'capybara/dsl'
require 'capybara/cucumber'
require 'selenium-webdriver'
require 'site_prism'
require 'cucumber'
require_rel '../features/pages'
require_rel '../features/classes'
World(Capybara::DSL)
World(Capybara::RSpecMatchers)
Login page
class LoginPage < SitePrism::Page
end
Login class
class Login
def initialize
#current_page = LoginPage.new
end
The error is being thrown on the line with "class LoginPage < SitePrism::Page". RubyMine can't find the SitePrism declaration to go to either. Am I missing something in the setup?
Your error looks 'require' related, but here is how I got it to work via:
https://github.com/thuss/standalone-cucumber
Haven't used cucumber in a while, but the way I see "pages" currently implemented in my Rails project:
Create a file in "features/support/pages"
Follow the namespace conventions
Use modules, then import via the World() method.
Maybe this might work:
features/support/pages/login_page.rb
module Pages
module LoginPage
class LoginPageObj < SitePrism::Page
end
def login_obj
LoginPageObj.new
end
end
end
World(Pages::LoginPage)
Env file:
require 'capybara'
require 'capybara/cucumber'
require 'site_prism'
Capybara.configure do |config|
config.default_driver = :selenium
config.app_host = 'http://www.google.com'
end
World(Capybara::DSL)
World(Capybara::RSpecMatchers)
Notice how I didn't have to explicitly require any pages class, it looks like Cucumber might require it for you?
Note this is without RubyMine (I dont use it). If it works without RubyMine, I'd point fingers to that.
So the reason you were getting this problem is because of the files being auto-loaded. Just ensure you require the gem files first so the namespaces are understood
In Rails we can use Minitest with this syntax:
test "true" do
assert true
end
When I try using this syntax in my gem I get an ArgumentError (wrong number of arguments).
To be clear, I don't care for the rspec syntax (I prefer assert over describe blocks). I only want to write test "foo" do instead of def test_foo.
How can I do this?
My test_helper.rb is almost empty:
gem 'minitest'
require 'minitest/autorun'
require 'foo/bar'
This is an ActiveSupport thing as far as I know, not a Minitest thing
https://github.com/rails/rails/blob/master/activesupport/lib/active_support/testing/declarative.rb
A quick thing you could do is require 'minitest/spec' and alias it as test
How to TDD the following snippet in Ruby using Eclipse on Windows?
class PassIntegers
def addition(i,j)
k = i+j
end
end
Install test-unit by executing gem install test-unit
Run the following snippet as 2 Ruby Test
require 'test/unit'
require 'PassIntegers'
class TestPassIntegers < Test::Unit::TestCase
def test_pass_integers
passIntegers = PassIntegers.new
expected = passIntegers.addition 3,2
assert_equal expected, 5
end
end
Possible issue
If the following error occurs at the Eclipse Console:
C:/dev/tools/eclipse/configuration/org.eclipse.osgi/bundles/965/1/.cp/testing/dltk-testunit-runner.rb:252:in `block in <top (required)>': uninitialized constant Test::Unit::UI::SILENT (NameError)
Open C:\dev\tools\eclipse\configuration\org.eclipse.osgi\bundles\965\1\.cp\testing\dltk-testunit-runner.rb and solve this by commenting
#autoRunner.output_level = Test::Unit::UI::SILENT
and adding
autoRunner.runner_options[:output_level] = Test::Unit::UI::Console::OutputLevel::SILENT
and run the test again.
Solution to solve the uninitialized constant Test::Unit::UI::SILENT (NameError) issue found at http://blog.christopherkaiser.de/?p=319
I'm trying to get Capybara running in a simple Ruby script -- i.e. without/outside of Rails. Here's the script:
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
include Capybara
Capybara.current_driver = :selenium
Capybara.app_host = 'http://www.google.com'
visit('/')
The problem is that when I run this I get this error:
NameError: uninitialized constant Capybara::Session
at top level in dsl.rb at line 52
method gem_original_require in custom_require.rb at line 36
method require in custom_require.rb at line 36
at top level in capybara_test.rb at line 3
method gem_original_require in custom_require.rb at line 31
method require in custom_require.rb at line 31
at top level in capybara_test.rb at line
What am I doing wrong?
Some more info:
Mac OS X 10.5
ruby 1.8.6 (2009-06-08 patchlevel 369) [universal-darwin9.0]
capybara (0.3.9)
Thanks!
Neal
Note: Per the comment from jnicklas I tried this, which matches the new README more closely:
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
Capybara.default_driver = :selenium
Capybara.app_host = 'http://www.google.com'
module MyCapybaraTest
include Capybara
def test_google
visit('/')
end
end
Unfortunately, I'm still seeing the same error:
NameError: uninitialized constant Capybara::Session
Thoughts?
Thanks!
Here's something that seems to work for me:
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
Capybara.run_server = false
Capybara.current_driver = :selenium
Capybara.app_host = 'http://www.google.com'
module MyCapybaraTest
class Test
include Capybara::DSL
def test_google
visit('/')
end
end
end
t = MyCapybaraTest::Test.new
t.test_google
It goes to show that even incorrect documentation lives forever. The Capybara README used to recommend to include Capybara in the global namespace, this is a really bad idea, and messes up any number of random things. You should include Capybara in your own module or class and use that instead.
Check out the README for current best practices.
Please check this CapybaraRspec101 example and fork it.
It's a small example for acceptance tests on http://www.hi5.com using from scratch:
Capybara
Rspec
Selenium-webdriver
All instructions are in the repo