[旁白:我正在慢慢回答我自己的问题" 在autotest或Guard下安装,配置和运行minitest的简明配方 "]
我的环境:Ruby 2.0.帕德里诺0.10.7.最小2.6.2.RackTest 0.6.2.
简短形式:扩展$ LOAD_PATH以包含我的测试目录的最佳方法是什么,所以我可以简单地require 'test_helper'
在我的测试文件中?
长表:
这是一个示例测试文件.注意require_relative "../../../test_helper"
- 这需要跟踪相对于test_helper的测试文件.
# file: test/models/api/v0/index_test.rb
require_relative '../../../test_helper'
describe 'nobody home' do
it 'fetch fails' do
get "/api/v0/a_uri_that_does_not_exist"
last_response.status.must_equal 404
end
end
Run Code Online (Sandbox Code Playgroud)
这是测试助手:
# file: test/test_helper.rb
PADRINO_ENV = 'test' unless defined?(PADRINO_ENV)
require File.expand_path('../../config/boot', __FILE__)
class MiniTest::Unit::TestCase
include Rack::Test::Methods
def app
Demo.tap { |app| }
end
end
Run Code Online (Sandbox Code Playgroud)
最后,驱动它的rakefile(由padrino生成,通过调用padrino rake test
):
# file: test/test.rake
require 'rake/testtask'
test_tasks = Dir['test/*/'].map { |d| File.basename(d) }
$stderr.puts("=== test_tasks = …
Run Code Online (Sandbox Code Playgroud) 根据这篇博客文章,我想找出测试套件中最慢的那些。这是代码的简化版本:
# test/test_time_tracking.rb
module TestTimeTracking
class ActiveSupport::TestCase
setup :mark_test_start_time
teardown :record_test_duration
def mark_test_start_time
@start_time = Time.now
end
def record_test_duration
puts "Test class: #{self.class.name}"
puts "Duration: #{Time.now - @start_time}"
end
end
end
# test/test_helper.rb
require 'test_time_tracking'
include TestTimeTracking
# ...
Run Code Online (Sandbox Code Playgroud)
是否可以在设置或拆卸期间打印出测试名称?在博客文章中,他们name
在teardown块中调用attribute,但这在我的情况下会引发错误。我也试着@name
和@method_name
没有成功。
我shoulda-contexts
在默认的Rails测试框架上使用gem。我知道可以使用来获取测试名称和持续时间rake test TESTOPTS=-v
,但是随后我将不得不运行另一个脚本来解析输出。
看起来acts-as-taggable-on 在它设置的数据表中使用了多态关系。我如何让它与 minitest 中的装置一起工作?
我有一个Lead
实现的模型acts-as-taggable
。看起来这将以下内容添加到模型中:
has_many :taggings, :as => :taggable, :dependent => :destroy, :class_name => "ActsAsTaggableOn::Tagging"
has_many :base_tags, :through => :taggings, :source => :tag, :class_name => "ActsAsTaggableOn::Tag"
Run Code Online (Sandbox Code Playgroud)
我的线索表:
create_table "leads", force: true do |t|
t.string "name"
t.string "email"
t.string "phone"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
Run Code Online (Sandbox Code Playgroud)
由acts-as-taggable-on 创建的表:
create_table "taggings", force: true do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context", limit: 128
t.datetime "created_at"
end
add_index "taggings", ["tag_id", "taggable_id", …
Run Code Online (Sandbox Code Playgroud) 如何仅使用 update_column 命令更新夹具的列以供临时使用。现在我有以下运行良好的命令:
name = names(:one)
role = roles(:one)
name.role_id = role.id
assert name.save
Run Code Online (Sandbox Code Playgroud)
它运行良好,但是有没有什么有效的方法可以在一行中做到这一点,例如 name.update_column(---, ----) ?
我想根据正则表达式检查导航中包含的每个链接。在使用如下代码之前,我已经检查了相同的导航是否有各种链接:
assert_select "nav.active" do |nav|
assert_select nav, "a[href=?]", edit_post_path(post), count: 0
end
Run Code Online (Sandbox Code Playgroud)
效果很好。我无法使用正则表达式执行类似的操作,如文档中所示。我尝试过使用这些变体(它们都故意注释掉):
assert_select "nav.active" do |nav|
#assert_select "a[href=?]", /.+/
#assert_select nav, "a[href=?]", /foo/, count: 1
end
Run Code Online (Sandbox Code Playgroud)
失败并输出:
Minitest::Assertion: Expected exactly 1 element matching "a[href=/.+/]", found 0..
Run Code Online (Sandbox Code Playgroud)
或者
Minitest::Assertion: Expected exactly 1 element matching "a[href=/foo/]", found 0..
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
如何测试方法的参数?
def create_person child
end
Run Code Online (Sandbox Code Playgroud)
上面的代码是我的方法.它需要一个名为"child"的参数.我尝试测试这个参数.所以,如果方法没有参数,测试会给我错误.我使用minitest和Ruby on Rails.
当我使用minitest-rails gem运行非常简单的测试时,我陷入了错误。我有Rails 4.1.5和minitest 5.4.0
抽水测试:控制器
1)错误:DashboardController :: index action#test_0001_anonymous:NoMethodError:“中的未定义方法get' for #<#<Class:0x00000008e28170>:0x00000008eeb9b8>
test/controllers/dashboard_controller_test.rb:6:in
块(3个级别)
测试:
require "test_helper"
describe DashboardController do
context "index action" do
before do
get :index
end
it { must_respond_with :success }
it "must render index view" do
must_render_template :index
end
end
end
Run Code Online (Sandbox Code Playgroud)
我的test_helper:
ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"
require "minitest/rails/capybara"
class MiniTest::Spec
class << self
alias :context :describe
end
end
class RequestTest < MiniTest::Spec
include Rails.application.routes.url_helpers
register_spec_type(/request$/, self)
end
class ActionDispatch::IntegrationTest
# …
Run Code Online (Sandbox Code Playgroud) 我的模型中有一个before_validation回调。我无法找到如何在minitest rails中编写回调的测试用例。
test 'callback set_slug before_validation' do
company = Company.new(name: 'test')
mock_method = MiniTest::Mock.new
mock_method.expect :set_slug, 'clickapps1'
company.stub :set_slug, 'clickapps1' do
company.valid?
end
mock_method.verify
end
Run Code Online (Sandbox Code Playgroud) 希望有人可以帮助我。我确实进行了搜索,但没有找到任何有效的解决方案。
我已经开始为应用编写测试。我的集成测试运行正常,但后来我决定,那么多的TDD的驱动,因为我没有,因为我没有那么多时间,现在要进行广泛的测试应用程序的所有层,我应该使用的,而不是integration
检验system
测试,因为它们使我可以像在浏览器中一样测试整个流程。
Rails 5.1.2
Gemfile
(尝试了不同的变化,只是水豚,然后结合了其他两个)
gem 'minitest-rails'
gem 'minitest-rails-capybara'
gem 'capybara'
Run Code Online (Sandbox Code Playgroud)
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
EMPTY_NEW_USER = {
email: '',
first_name: '',
last_name: '',
username: '',
password: ''
}
EXISTING_USER = {
email: '****',
first_name: 'John',
last_name: 'Doe',
username: '',
password: 'testingpass',
password_confirmation: 'testingpass'
}
# Add more helper methods to be used by …
Run Code Online (Sandbox Code Playgroud)