我正在将我的测试移动到新的ruby minitest库,我正在寻找与旧的Test :: Unit :: TestSuite类相对应的类.我在网上找到的所有例子都显示了单个测试用例,但我得到了:
require 'minitest/unit/testsuite'
require 'minitest/unit/ui/console/testrunner'
require 'tests/fs_session_test'
require 'tests/resource_test'
require 'tests/rest_session_test'
require 'tests/server_test'
class AllTests
def self.suite
suite = Test::Unit::TestSuite.new
suite << FSSessionTest.suite
suite << ResourceTest.suite
suite << RESTSessionTest.suite
suite << ServerTest.suite
end
end
Test::Unit::UI::Console::TestRunner.run(AllTests)
Run Code Online (Sandbox Code Playgroud)
我一直在testsuite require上得到一个LoadError.
我遇到了与路由规范中未定义的方法`assertions'相同的问题,但是我正在运行Rails 4并且它被锁定为minitest 5.0或更高版本.因此使用minitest 4.7的解决方案我无法工作.有没有解决这个问题?我在哪里报告错误?
我知道RSpec有--profile选项,但我只是将MiniTest/shoulda用于我当前的项目.
我有以下代码用于模拟对类方法的调用:
def test_calls_update_profile_job_for_a_lead
input = ContactInput.new valid_attributes
mock = MiniTest::Mock.new
use_case = CreateContact.new user, input, mock
mock.expect(:perform_async, nil, [user.id, 1, ::Contact])
use_case.run!
assert mock.verify
end
Run Code Online (Sandbox Code Playgroud)
问题是我必须传递具体的价值 -
[user.id,1,:: Contact]
使测试通过.
有没有办法我不必指定确切的值,或者至少约束参数是什么.我不想检查确切的参数,我只是想确保调用该方法.
MiniTest并行运行我的测试用例.有没有办法强制按顺序运行测试用例?
def test_1
end
def test_2
end
Run Code Online (Sandbox Code Playgroud)
如何在test_2之前强制test_1运行?
我正在尝试使用Minitest中的自定义标头发送请求:
get '/api/v1/tasks/all', nil, {:authorization => 'sdlfjkvnsdfljn'}
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,请求标头未被发送.这是价值request.headers.env:
"{"rack.version"=>[1, 2], "rack.input"=>#<StringIO:0x000000055a55f0>, "rack.errors"=>#<StringIO:0x000000055a5708>, "rack.multithread"=>true, "rack.multiprocess"=>true, "rack.run_once"=>false, "REQUEST_METHOD"=>"GET", "SERVER_NAME"=>"example.org", "SERVER_PORT"=>"80", "QUERY_STRING"=>"", "PATH_INFO"=>"/api/v1/tasks/all", "rack.url_scheme"=>"http", "HTTPS"=>"off", "SCRIPT_NAME"=>"", "CONTENT_LENGTH"=>"0", "rack.test"=>true, "REMOTE_ADDR"=>"127.0.0.1", "HTTP_HOST"=>"example.org", "HTTP_COOKIE"=>"", "ORIGINAL_FULLPATH"=>"/api/v1/tasks/all", "ORIGINAL_SCRIPT_NAME"=>"", "action_dispatch.routes"=>#<ActionDispatch::Routing::RouteSet:0x00000005183788>, "action_dispatch.parameter_filter"=>[:password], "action_dispatch.redirect_filter"=>[], "action_dispatch.secret_token"=>"21a9fdaa0b07f7e418e9a7c838e124fd1f3fbd2b65fb1520f8d38e10d643ce33cc46995f359bd38bfbc9335905fbdeb8af6879ebae131d3fe76f69a9619f6b37", "action_dispatch.secret_key_base"=>"a021d8b08999858216f202033b8124c4c8ec34590c3c62bec68e08e37526176c64d3d396b1e9109f74966b22056a65f557d6fd761093992d5641eb8a741bfc39", "action_dispatch.show_exceptions"=>false, "action_dispatch.show_detailed_exceptions"=>true, "action_dispatch.logger"=>#<ActiveSupport::Logger:0x00000005103e70 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x00000005103df8 @datetime_format=nil>, @formatter=#<ActiveSupport::Logger::SimpleFormatter:0x00000005c487b8 @datetime_format=nil>, @logdev=#<Logger::LogDevice:0x00000005103da8 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<File:/home/sergey/Desktop/git2/bo.crmtronic.com/log/test.log>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x00000005103d80 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x00000005103d30>>>>, "action_dispatch.backtrace_cleaner"=>#<Rails::BacktraceCleaner:0x000000050f0d48 @filters=[#<Proc:0x000000050f0c80@/home/sergey/.rvm/gems/ruby-2.1.2/gems/railties-4.1.1/lib/rails/backtrace_cleaner.rb:10>, #<Proc:0x000000050f0c30@/home/sergey/.rvm/gems/ruby-2.1.2/gems/railties-4.1.1/lib/rails/backtrace_cleaner.rb:11>, #<Proc:0x000000050f0c08@/home/sergey/.rvm/gems/ruby-2.1.2/gems/railties-4.1.1/lib/rails/backtrace_cleaner.rb:12>, #<Proc:0x000000050f03c0@/home/sergey/.rvm/gems/ruby-2.1.2/gems/railties-4.1.1/lib/rails/backtrace_cleaner.rb:24>], @silencers=[#<Proc:0x000000050f0398@/home/sergey/.rvm/gems/ruby-2.1.2/gems/railties-4.1.1/lib/rails/backtrace_cleaner.rb:15>]>, "action_dispatch.key_generator"=>#<ActiveSupport::CachingKeyGenerator:0x000000055a0230 @key_generator=#<ActiveSupport::KeyGenerator:0x000000055a0280 @secret="a021d8b08999858216f202033b8124c4c8ec34590c3c62bec68e08e37526176c64d3d396b1e9109f74966b22056a65f557d6fd761093992d5641eb8a741bfc39", @iterations=1000>, @cache_keys=#<ThreadSafe::Cache:0x000000055a0208 @backend={"encrypted cookie64"=>"\xD6Q\xF1\xD9\xA6w\x18\x80\xB1,\xE3\xB6\x8EQ\xC0'.-\xE5\xA3\x80$\xD8\xAD\xD8\n\xDD\x84\xCED\xE4q\xDA\xC8W\xB5\xE7m\xE8a\xC6\xD8\r\xA0\xA1Q\xC5ug\xD5R\x9C\x96\xE7vIZC\xAC\xEB\x92h\b\x89", "signed encrypted cookie64"=>" \xA2\xAF\t\xADr_\x1A\x87q\x00\xC0\x8DO2\xEF\x1ElZ5\xB6\xBAS\x9Em\x16crM\xEC\xC5\xEF\xD4\x8E\xDCC\xDB\xA5K\xE88\xCFl\x1F\x86\e\x99\xC0\xD0\xEF\xB9:\xAA\xE2Y\xB9h\xF3\xB4Mz\x9E] "}, @default_proc=nil>>, "action_dispatch.http_auth_salt"=>"http authentication", "action_dispatch.signed_cookie_salt"=>"signed cookie", "action_dispatch.encrypted_cookie_salt"=>"encrypted cookie", …Run Code Online (Sandbox Code Playgroud) 我只想为一个测试和其他测试添加一个类方法,我想调用实际方法。我一直在使用rspec和mocha,因此以下行为看起来很奇怪。
我想在一项测试中存根的课程。
class MyClass
def self.foo(arg)
return "foo#{arg}"
end
end
Run Code Online (Sandbox Code Playgroud)
我尝试对MyClass.foo进行存根的测试
class XYZTest < Minitest::Test
def test_1
MyClass.expects(:foo).returns('abcd')
assert_equal MyClass.foo('123'), 'abcd'
end
def test_2
assert_equal MyClass.foo('123'), 'foo123'
end
end
Run Code Online (Sandbox Code Playgroud)
第一次测试通过,但是第二次测试失败,说明Mocha :: ExpectationError:意外调用:MyClass.foo('123')
在test_2中,我希望调用实际的类方法,而不是我在test_1中执行的存根。
PS:上面是一个简化的示例。我不想每次都重置,所以我对类方法进行了设置。
希望对MiniTest的人们来说是一个简单的问题。
我有一段代码,将在这里浓缩为一个示例:
class Foo
def initialize(name)
@sqs = Aws::SQS::Client.new
@id = @sqs.create_queue( queue_name: name ).fetch(:queue_url)
@poller = Aws::SQS::QueuePoller.new(@id)
end
def pick_first
@poller.poll(idle_timeout: 60) do |message|
process_msg(message) if some_condition(message)
end
end
Run Code Online (Sandbox Code Playgroud)
我该如何模拟/存根/其他方式,以便我可以提供一个message通行证以供测试,some_condition()并可能通过处理process_msg()?
即我要测试@poller.poll(idle_timeout: 60) do |message|。
我试图Aws::SQS::QueuePoller#new用模拟轮询器对Stub进行存根,但是它不会产生|message|仅返回它的消息。
这就是我,这是不工作:
mockqueue = MiniTest::Mock.new
mocksqs = MiniTest::Mock.new
mocksqs.expect :create_queue, mockqueue, [Hash]
mockpoller = MiniTest::Mock.new
mockpoller.expect :poll, 'message', [{ idle_timeout: 60 }]
Aws::SQS::Client.stub :new, mocksqs do
Aws::SQS::QueuePoller.stub :new, mockpoller do
queue …Run Code Online (Sandbox Code Playgroud) 我无法使用minitest在我的Rails 4.2应用程序中成功测试自定义HTTP变量 - 出于某种原因,Rails似乎根本不识别它们.
我有以下测试:
require "test_helper"
describe ApplicationController do
tests HomeController
before do
cookies[:locale] = "ru"
request.headers["Accept-Language"] = "zh-cn, en-us"
request.headers["CloudFront-Viewer-Country"] = "FR"
I18n.default_locale = :en
end
it "sets locale from URL params first" do
get :index, locale: "de"
I18n.locale.must_equal :de
end
it "sets locale from a cookie second" do
get :index
I18n.locale.must_equal :ru
end
it "sets locale from Accept-Language header third" do
cookies.delete :locale
get :index
I18n.locale.must_equal :"zh-CN"
end
it "sets locale from CloudFront-Viewer-Country header last" do
cookies.delete …Run Code Online (Sandbox Code Playgroud) minitest ×10
ruby ×6
unit-testing ×4
omniauth ×1
rspec-rails ×1
rspec2 ×1
sequel ×1
shoulda ×1
tdd ×1