我已阻止在 Minitest 文件中重构我的代码。
我有这个例子:
test_calls.rb
<select class="input-validation-error form-control" data-val="true" data-val-number="The field Membership Types must be a number." data-val-required="The Membership Types field is required." id="Customer_MemberShipTypeId" name="Customer.MemberShipTypeId"><option value="">Select Membership Type</option>
<option value="1">Pays as You Go</option>
<option value="2">Monthly</option>
<option value="3">Quaterly</option>
<option value="4">Yearly</option>
</select>
test_users.rb
describe Aircall::Calls do
before do
@call_by_id = DefaultAircall::AIRCALL.calls.get_by_id(34043501)
@call_by_user_id = DefaultAircall::AIRCALL.calls.get_by_user_id(80603)
end
describe "By id" do
it "is Hash" do
@call_by_id.must_be_instance_of Hash
end
it "no error return" do
@call_by_id['error'].must_be_nil
end
end
describe "by user id" do
it "is Hash" do
@call_by_user_id.must_be_instance_of Hash
end
it "no error return" do
@call_by_user_id['error'].must_be_nil
end
end
end
理想情况下,我希望将我的代码拆分到另一个文件中,如下所示:
describe Aircall::Users do
describe "By id" do
before do
@user_by_id = DefaultAircall::AIRCALL.users.get_by_id(80603)
end
it "is Hash" do
@user_by_id.must_be_instance_of Hash
end
it "no error return" do
@user_by_id['error'].must_be_nil
end
end
end
所以,我可以在我的不同测试文件中导入默认测试。
我不知道它是否可能以及如何实现。我认为我已经成功了一次,但测试不会出现在控制台中。如果我调用运行Minitest测试的类方法,我的任务(运行测试的人)不会考虑我的默认测试。
好主意Keith Bennett! Lambda是一个很好的方法。
我现在有了解决方案:
我的模块与lambda运行默认测试: 的 default_tests.rb
class DefaultTest
def initialize(element_to_test)
@element_to_test = element_to_test
end
def defaults_tests
describe "default" do
it "is Hash" do
@element_to_test.must_be_instance_of Hash
end
it "no error return" do
@element_to_test['error'].must_be_nil
end
end
end
end
test_contacts.rb
module DefaultTest
Run = ->(*variables_to_test) do
describe "Default test" do
variables_to_test.each do |variable|
it "is Hash" do
self.class.send(variable).must_be_instance_of Hash
end
it "no error return" do
self.class.send(variable)['error'].must_be_nil
end
end
end
end
end
test_users.rb
module TestAircall
describe Aircall::Contacts do
def self.contact_by_id
@contact_by_id ||= DefaultAircall::AIRCALL.contacts.get_by_id(ENV['TEST_DEFAULT_CONTACT_ID'])
end
def self.contact_by_phone_number
@contact_by_phone_number ||= DefaultAircall::AIRCALL.contacts.get_by_phone_number(ENV['TEST_DEFAULT_PHONE_NUMBER'])
end
def self.contact_by_email
@contact_by_email ||= DefaultAircall::AIRCALL.contacts.get_by_email(ENV['TEST_DEFAULT_EMAIL'])
end
DefaultTest::Run.('contact_by_id', 'contact_by_phone_number', 'contact_by_email')
end
end
我可以在任何变量的每个文件测试中调用并运行我的默认测试。
答案 0 :(得分:0)
据我所知,你不可能做你想做的事。这里的问题是it
只能在describe
块内调用。 <{1}}将无法在该块中名为的方法中使用。
我做了一些实验。我能想到的最好的是lambda可以在it
块中定义,并由describe
中的所有it
块使用。 (以这种方式不可能使用传统方法。)
以下是一个例子:
describe
调用require "minitest/autorun"
class MyTest < Minitest::Test
describe 'number test' do
# It is not legal to define a method here,
# you'll get this error when trying to call it:
# "undefined method `is_odd'"
# def is_odd(number)
# it 'is odd' do
# assert_equal (number / 2), 1
# end
# end
is_odd_lambda = ->(number) do
it 'is odd' do
assert_equal (number / 2), 1
end
end
# is_odd(3)
is_odd_lambda.(3)
end
end
方法会导致错误,但调用is_odd
则不会。
答案 1 :(得分:0)
好主意Keith Bennett! Lambda是一个很好的方法。
我现在有了解决方案:
我的模块与lambda运行默认测试: 的 default_tests.rb 强>
module DefaultTest
Run = ->(*variables_to_test) do
describe "Default test" do
variables_to_test.each do |variable|
it "is Hash" do
self.class.send(variable).must_be_instance_of Hash
end
it "no error return" do
self.class.send(variable)['error'].must_be_nil
end
end
end
end
end
<强> test_contacts.rb 强>
module TestAircall
describe Aircall::Contacts do
def self.contact_by_id
@contact_by_id ||= DefaultAircall::AIRCALL.contacts.get_by_id(ENV['TEST_DEFAULT_CONTACT_ID'])
end
def self.contact_by_phone_number
@contact_by_phone_number ||= DefaultAircall::AIRCALL.contacts.get_by_phone_number(ENV['TEST_DEFAULT_PHONE_NUMBER'])
end
def self.contact_by_email
@contact_by_email ||= DefaultAircall::AIRCALL.contacts.get_by_email(ENV['TEST_DEFAULT_EMAIL'])
end
DefaultTest::Run.('contact_by_id', 'contact_by_phone_number', 'contact_by_email')
end
end
<强> test_users.rb 强>
module TestAircall
describe Aircall::Users do
def self.user_by_id
@user_by_id ||= DefaultAircall::AIRCALL.users.get_by_id(ENV['TEST_DEFAULT_USER_ID'])
end
DefaultTest::Run.('user_by_id')
end
end
我可以在任何变量的每个文件测试中调用并运行我的默认测试。