My test fails when trying to find the test_helper file.
My file:
require File.dirname(__FILE__) + '/../test_helper'
require 'admin/supplier_controller'
#Re-raise errors caugth by the controller
class Admin::SupplierController; def rescue_action(e) raise e end; end
class Admin::SupplierControllerTest < ActionController::TestCase
fixtures :suppliers
def setup
#controller = Admin::SupplierController.new
#request = ActionController::TestRequest.new
#response = ActionController::TestResponse.new
end
def test_new
get :new
assert_template 'admin/supplier/new'
assert_tag 'h1', :content => 'Create new supplier'
assert_tag 'form', :attributes => {:action => '/admin/supplier/create'}
end
end
No need to do this:
require File.dirname(__FILE__) + '/../test_helper'
Just require 'test_helper' on the top of the test file, and add -Itest option to run the test. (assuming test_helper.rb is located in test/ directory)
Related
How to test or stub Sidekiq::Batch in RSPEC ?
Please see got error in code below.
rails_helper
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'sidekiq/testing'
Sidekiq::Testing.fake!
Worker to be tested...
class TestWorker
def perform(id)
batch = Sidekiq::Batch.new
batch.description = "Sample"
batch.on(:complete, TestWorker, 'id' => 123, 'last_checked' => Time.now)
# => *** NoMethodError Exception: undefined method `on' for #<Batch:0x007fd8b728a1d0>
batch.jobs do # => NoMethodError: undefined method `jobs' for #<Batch:0x007f936c9ebf68>
Sidekiq::Client.push({
'class' => TestWorker,
'queue' => q,
'args' => [id, batch.bid, 1, Time.now]
})
end
end
end
class TestWorkerCallbacks
def complete(status, options)
#simple record update here
end
end
spec/workers/test_worker_spec.rb
require 'rails_helper'
RSpec.describe TestWorker, type: :worker do
context "Sidekiq Worker" do
it "should respond to #perform" do
expect(TestWorker.new).to respond_to(:perform)
end
describe "perform" do
before do
Sidekiq::Worker.clear_all
end
it "updates order records" do
expect(TestWorker.jobs.size).to eq(0)
TestWorker.perform_async(123)
TestWorker.drain
expect(TestWorker.jobs.size).to eq(1)
end
end
end
end
I'm preparing some integration tests on my rails 3.2.16 application, I figured that, in my user scenarios, I have several calls that I repeat over many tests, so I would like to DRY them up, by placing them in a separate common module,
for example I have created /test/integration/my_test_helpers.rb:
require 'test_helper'
module MyTestHelper
def login_user(email, password, stay = 0)
login_data = {
email: email,
password: password,
remember_me: stay
}
post "/users/sign_in", user: login_data
assert_redirected_to :user_root
end
end
and tried to use it in my integration test:
require 'test_helper'
require "./my_test_helpers.rb"
class CreateCommentTest < ActionDispatch::IntegrationTest
setup do
#user = users(:user1)
end
test "create comment" do
login_user #user.email, "password", 1
end
end
I get exception:
`require': cannot load such file -- ./my_test_helpers.rb (LoadError)
How can I load the module? is it right to make MyTestHelpers a module?
You should put your helper in support folder(test/support/my_test_helpers.rb, or something) and load module in test_helper.rb:
ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require_relative "./support/my_test_helpers"
require "minitest/rails"
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
fixtures :all
# Add more helper methods to be used by all tests here...
end
Do not remember include your module:
require 'test_helper'
class CreateCommentTest < ActionDispatch::IntegrationTest
include MyTestHelper
setup do
#user = users(:user1)
end
test "create comment" do
login_user #user.email, "password", 1
end
end
When i try to execute a functional test with this command
ruby -I. supplier_controller_test.rb
now the code:
require File.dirname(__FILE__) + '/../../test_helper'
require 'admin/supplier_controller'
# Re-raise errors caught by the controller.
class Admin::SupplierController; def rescue_action(e) raise e end; end
class Admin::SupplierControllerTest < ActionController::TestCase
fixtures :suppliers
def setup
#controller = Admin::SupplierController.new
#request = ActionController::TestRequest.new
#response = ActionController::TestResponse.new
end
def test_new
get :new
assert_template 'admin/supplier/new'
assert_tag 'h1', :content => 'Create new supplier'
assert_tag 'form', :attributes => {:action => '/admin/supplier/create'}
end
end
this is the error that i get
No such file or directory - ./log/acts_as_ferret.log (Errno::ENOENT)
It's supposed that should exists an acts_as_ferret.log?
When you run ruby -I . path_to_code with the -I option you are supplying the directory to load libraries from.
From the error it looks like you are trying to load a log/acts_as_ferret.log from the directory that you just specified.
Is it possible that you want to be using a different directory?
Getting the following when running my capybara rails tests with "rake test". Problem seems to be url helpers I'm using in my application.html.erb:
DashboardLoginTest#test_login_and_check_dashboard:
ActionView::Template::Error: arguments passed to url_for can't be handled. Please require routes or provide your own implementation
app/views/layouts/application.html.erb:29:in `_app_views_layouts_application_html_erb__938277815294620636_4045700'
test/integration/dashboard_login_test.rb:6:in `block in <class:DashboardLoginTest>'
Here's line 29 in application.html.erb that its complaining about:
<%= link_to("asdf", root_path, {:class => 'brand'}) %>
Here's what the test looks like:
test "login and check dashboard" do
visit("/")
assert page.has_content?("welcome")
end
Here's my test_helper.rb:
ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'factory_girl'
require 'capybara/rails'
include Warden::Test::Helpers
Warden.test_mode!
def main_app
Rails.application.class.routes.url_helpers
end
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
fixtures :all
end
class ActionDispatch::IntegrationTest
# Make the Capybara DSL available in all integration tests
include Capybara::DSL
include Rails.application.routes.url_helpers
def sign_in(user = nil)
if user.nil?
user = FactoryGirl.create(:user)
$current_user = user
end
login_as(user, :scope => :user)
user.confirmed_at = Time.now
user.save
end
end
Trying to learn Ruby and below is a module I have created in order to test IO Ruby features. When I run the following tests:
subject{TestGem.new}
it 'should be_a Module' do
subject.is_a Module
end
it 'creates a config file' do
subject.init_config
File.open("program.config", "r") { |io| io.readline }.should eq "default_file.txt"
end
I get this error for both:
Failure/Error: subject{TestGem.new}
NoMethodError:
undefined method `new' for TestGem:Module
Here is the Module I am testing. Any help/suggestions would greatly appreciated :)
$LOAD_PATH.unshift File.expand_path("../test_gem", __FILE__)
require 'version'
require 'hello'
module TestGem
#default_file_name
#supported_types
def initialize
default_file_name = 'default_file.txt'
supported_types = ['txt', 'pdf']
end
puts "module TestGem defined"
def self.init_config
File.open('program.config', "w") do |f|
f.write(yaml(#default_file_name))
f.write(yaml(#supported_types))
end
end
class MyFile
def self.first(filename)
File.open(filename, "r") {|f| f.readline}
end
def self.last(filename)
File.open(filename, "r")[-1]
end
end
class MyError < StandardError
puts "Standard Error"
end
end
Short answer: You can't instantiate modules objects
module A
end
class B
end
B.methods - A.methods #=> [:new, :superclass, :allocate]
To test a module you can include it in a object like this
object = Object.new
object.extend(TestGem)
Or you can create some example class if your module depends on some class behavior.