Why do i need to use minitest/autorun? - ruby-on-rails

Why do i require minitest/autorun instead of test/unit for generating unit test
require 'test/unit'
class Brokened
def uh_oh
"I needs fixing"
end
end
class BrokenedTest < Minitest::Test
def test_uh_of
actual = Brokened.new
assert_equal("I'm all better now", actual.uh_oh)
end
end
Running the above code, interpreter raise warning
You should require 'minitest/autorun' instead

Your code example will end in a NameError: uninitialized constant Minitest.
You have two possibilities:
Use test/unit in combination with Test::Unit::TestCase or
use require 'minitest/autorun' in combination with Minitest::Test.
test/unit is deprecated and it is recommended to use minitest (MiniTest is faster and smaller).
If you switch the test gem you must change perhaps some more things:
replace require "test/unit" with require "minitest/autorun"
replace Test::Unit::TestCase with with Minitest::Test
There is no assert_nothing_raised (details)
assert_raise becomes assert_raises.
perhaps some other issues
You may use require 'minitest' instead require 'minitest/autorun' - you will get no syntax error, but there is also no test execution. If you want to execute tests, you must call them on your own (see minitest-a-test-suite-with-method-level-granularity)

Related

Use BestInPlace::TestHelpers with minitest

I'm trying to use this line in a minitest test that uses capybara, poltergeist, and phantomjs:
bip_select(#gs, :goal_id, Goal.first.name)
This is a helper that best_in_place offers to simulate a user choosing a value from a field. I've read a few questions elsewhere on StackOverflow where other developers who are using RSpec have added this line to their spec_helper.rb file:
config.include BestInPlace::TestHelpers
I've tried adding this line to my test_helper.rb file and I've tried adding it to the test in question. But I'm still getting the error
NoMethodError: undefined method `bip_select' for #<GoalStudentsPoltergeistEditTest:0x00000006d85148>
Thank you in advance for any insight.
To get the method definition available in your integration tests, edit test_helper.rb to include the lines referring to Best in Place (other lines left for context):
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'best_in_place/test_helpers'
# ...
class ActionDispatch::IntegrationTest
include BestInPlace::TestHelpers
end

Fast Testing: How to test Mailer without Loading Rails

I've got some work to do refactoring a mailer class. I don't want to spend all my time loading rails and waiting to see if tests are passing. I'd like to just not include spec_helper and speed up my tests.
How do I just include ActionMailer?
I tried this:
require 'action_mailer'
but I stil end up with this error:
uninitialized constant ActionMailer (NameError)
Any ideas?
I'm using MiniTest, and the following works fine for me. You may be able to extrapolate from this, or provide some more info in your question.
require 'minitest/autorun'
require 'mocha'
require "minitest-matchers"
require 'action_mailer'
require "email_spec"
require File.expand_path('../../../../app/mailers/my_mailer', __FILE__)
class MyMailerTest < MiniTest::Unit::TestCase
include EmailSpec::Helpers
include EmailSpec::Matchers
def test_it_is_sent_from_me
email = MyMailer.refund_processed(42)
email.must be_delivered_from("me#example.com")
end
end

testing rails engine generator with rspec

I create a simpe gem wich include a install generator, generator works fine but now I want to test it using rspec, I foud this gem, and try to test my generator, my spec code is:
require 'genspec'
require 'rosalie'
describe :install_generator do
it "should generate model" do
subject.should generate("message.rb")
end
end
rosalie is the name of may gem, now when I run it I got an error:
/stuff/work/my_projects/rosalie/lib/rosalie/engine.rb:2:in `': uninitialized constant Rosalie::Rails (NameError)
my engine.rb code is:
module Rosalie
class Engine < Rails::Engine
initializer "rosalie.models.messageable" do
ActiveSupport.on_load(:active_record) do
include Rosalie::Models::Messageable
end
end
end
end
anybody can help me with this problem?
You have to load your code before you include it somewhere.
Either require or autoload your main file.
Here is an example from my gem.
You need add these code in your spec_helper.rb, and require the spec_helper in each spec.
require File.expand_path("../dummy/config/environment", __FILE__)
require 'rspec/rails'

RSpec: undefined local variable or method `activate_authlogic'

My _spec file includes the code below, but my test fails with:
NameError in 'MembershipsController should allow you to save updates to the notes'
undefined local variable or method `activate_authlogic' for #<Spec::Rails::Example::ControllerExampleGroup::Subclass_1:0x107cee930>
I don't understand why activate_authlogic is undefined in this case. I've used this line in TestUnit many times, and the RSpec examples I've read all seem to say that this should work. NOTE: I've also tried adding require 'authlogic' to the top of the _spec file, but it produces an identical error message.
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'ruby-debug'
describe MembershipsController do
before(:each) do
activate_authlogic
#admin = Factory(:admin, :email => "admin#example.com")
UserSession.create(#admin)
end
...
end
Apparently a misunderstanding on my part. Instead of require 'authlogic'
I needed require 'authlogic/test_case'

ConnectionNotEstablished error in custom TestTask

I'm trying to write a custom Rake task to perform some tests for a class placed in the lib directory. This works for basic tests not requiring any models, but I need to actually test using some models. It's my first foray into more advanced rake usage and after going through some other hurdles I've got stuck on getting a ConnectionNotEstablished error.
Here's the rake task:
Rake::TestTask.new(:test => 'db:test:prepare') do |test|
test.libs << 'test/sync'
test.test_files = Dir['test/sync/*_test.rb']
test.verbose = true
end
Here's the test that raise the ConnectionNotEstablished exception:
require 'rubygems'
require 'app/models/city'
require 'foo'
require 'test/unit'
class SyncTest < Test::Unit::TestCase
##sync = Foo::Sync.new('test')
def test_cities
assert City.all.size == 2 # the exception is raised at this point
##sync.cities
assert City.all.size == 102
end
end
Here's a unit test that is actually working:
require 'test_helper'
class CityTest < ActiveSupport::TestCase
test "the truth" do
assert City.all.size == 2
end
end
I've tried using derive my test class from ActiveSupport::TestCase instead of Test::Unit::TestCase but it still raise a ConnectionNotEstablished error. I'm certainly doing something wrong, can anyone find what or tell of a better way to do that?
I've finally found out what the problem was, I've replaced the two first require call by:
require 'test_helper'
and added the test directory to the TestTask's libs attribute:
test.libs << 'test'

Resources