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
Related
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
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)
When I try to run the following code:
test/models/tweet_test.rb
require 'minitest/spec'
require 'minitest/autorun'
class TweetTest < ActiveSupport::TestCase
test "Tweet should not be null" do
tweet = true
assert tweet
end
end
I receive this error:
tweet_test.rb:4:in `<main>': uninitialized constant ActiveSupport (NameError)
I am following along with a tutorial perfectly. Why is this happening? Has ActiveSuport::TestCase been deprecated?
UPDATE:
I attempted to require 'test_helper' :
require 'minitest/spec'
require 'minitest/autorun'
require 'test_helper'
class TweetTest < ActiveSupport::TestCase
test "Tweet should not be null" do
tweet = true
assert tweet
end
end
But received the following error:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- test_helper (LoadError)
You are not requiring the correct Rails minitest files to setup an ActiveSupport::TestCase. That is a Rails specific class that cannot be found in minitest alone. Are you sure you are working inside a Rails project?
In most cases, you setup a test in Rails by requiring test_helper in your test class so you can inherit from ActiveSupport::TestCase. The file test_helper contains all the requirements and setup for running tests in a Rails project.
Quick fix: just inherit from Minitest::Test.
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'
When running my specs with rspec & capybara, it can't find capybara's visit method. Is there another initialization step I need to do?
$bundle exec rspec spec
/home/brian/projects/expense_track/expense_track/spec/requests/homepage_spec.rb:6:in `block (2 levels) in <top (required)>': undefined method `visit' for #<Class:0xb6572b8> (NoMethodError)
Gemfile:
group :test, :development do
gem "rspec-rails"
gem "capybara"
end
top of my spec_helper.rb:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'rspec/autorun'
homepage_spec.rb:
require 'spec_helper'
describe "The home page" do
context "home page exists" do
visit "/"
page.should have_content("elephants")
end
end
Just ran into this issue myself.
So the reason for this is there has been a somewhat undocumented change in Capybara. Capybara now makes the assumption that anything using it needs to be in the spec/features folder and it will make the proper assumptions. Anything left in the spec/requests folder will no longer work. Though there are workarounds.
For a context block you can add the parameter :type => :feature and this will fix that issue or you can change the name of a describe method at the beginning of a spec to feature and this should change it as well.
They announced this change in their Google group: https://groups.google.com/forum/?fromgroups=#!topic/ruby-capybara/5KfxezI-U0Q
Notably, we changed the :type that Capybara assumes your specs run under in RSpec to :feature (previously it was :request). The latest release of spec/features. Alternatively you can use the Capybara Feature DSL (feature instead of describe), which should work without any additional tweaking. If you see errors like undefined method visit, then you're probably encountering this issue. If you're including modules into :request specs, you will probably need to change that to :feature.
This was further discussed in the github issue: https://github.com/jnicklas/capybara/issues/814
A few things to note here :
The changes in Capybara 2.0.x are documented here https://github.com/rspec/rspec-rails/blob/master/Capybara.md . There are changes in the spec directory structure : spec/features, spec/controllers, spec/views, spec/helpers, spec/mailers.
load Capybara dsl explicitly inside your spec_helper
require 'capybara/rails'
require 'capybara/rspec'
include Capybara::DSL
This worked for me.
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'capybara/rails'
RSpec.configure do |config|
config.include Capybara::DSL, :type => :request
end
This enables you to use Capybara's helpers inside spec/requests.
Because RSpec.configure not including capybara DSL in spec_helper.rb
It is an ugly solution, but you can add this to your spec_helper.rb.
module ::RSpec::Core
class ExampleGroup
include Capybara::DSL
include Capybara::RSpecMatchers
end
end
The git issue for this:
https://github.com/rspec/rspec-rails/issues/503
Unfortunately this workaround doesn't do it for me. I still get
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x007fbfeb535298>
The repo is public under: https://github.com/ikusei/Goldencobra_Newsletter
You need to look at the branch '28817499-subscribe'
edit: If i put include Capybara::DSL inside my describe block it works.
but including Capybara::DSL in the global scope is not recommended!
Because I do not know a good way.
For rspec 3 and rails, make sure you are using require "rails_helper", instead of require "spec_helper".
Otherwise, review the latest changes to rspec 3 & rspec-rails and Capybara 2.0.x.