我应该如何在RSPEC中验证此自定义匹配器
我最近开始研究RSPEC,并有一个解决挑战:做一个能够使用Regex识别一些键的对象。
该对象需要通过一系列RSPEC测试。 以下是一些示例:
describe '#phone?' do
context 'when receive a valid phone' do
[
'+55861107350',
].each do |key|
context "when key is #{key}" do
let(:key) { key }
it { is_expected.to be_a_phone }
end
end
end
context 'when receive an invalid phone' do
[
'55861107350',
'jake@gmail',
'123e4567-e89b-12d3-a456-426655440000'
].each do |key|
context "when key is #{key}" do
it { is_expected.not_to be_a_phone }
end
end
end
我找不到的是我应该如何验证be_a__phone匹配器,因为在挑战中说我只能修改类文件(这是空的,我需要创建),
我一直在研究自定义匹配器和定义自定义匹配器的方式是通过在另一个规格文件中创建方法,据说我不允许这样做。
我可以在班级内部制作RSPEC :: matchers.define:be_a_phone吗?还是类文件?
我的班级(非常原始):
# frozen_string_literal: true
class Key
attr_reader :key, :type
def initialize(key)
@key = key.to_s
@type = ''
self.define_type
end
def set_key(newKey)
@key = newKey
end
def set_type(newType)
@type = newType
end
def define_type
if self.key.scan(/^\+[1-9][0-9]\d{1,14}$/)[0] == self.key
puts "Your key is a valid Phone"
self.set_type("phone")
return "valid"
return self.set_key("invalid")
end
end
I have started studying Rspec recently and a got a challenge to solve: Do an object capable of recognize some keys using Regex.
The object needs to pass in a bunch of Rspec tests.
Here are some examples:
describe '#phone?' do
context 'when receive a valid phone' do
[
'+55861107350',
].each do |key|
context "when key is #{key}" do
let(:key) { key }
it { is_expected.to be_a_phone }
end
end
end
context 'when receive an invalid phone' do
[
'55861107350',
'jake@gmail',
'123e4567-e89b-12d3-a456-426655440000'
].each do |key|
context "when key is #{key}" do
it { is_expected.not_to be_a_phone }
end
end
end
What I'm not getting is how am I supposed to validate the be_a_phone matcher, since in the challenge says I can only modify the class file(which is empty and I need to create)
I have been researching about custom matchers and the way you define a custom matcher is by creating the method in another spec file, and supposedly I'm not allow to do that.
Can I make a Rspec::matchers.define :be_a_phone inside a class? or a class file?
My class(it's very raw):
# frozen_string_literal: true
class Key
attr_reader :key, :type
def initialize(key)
@key = key.to_s
@type = ''
self.define_type
end
def set_key(newKey)
@key = newKey
end
def set_type(newType)
@type = newType
end
def define_type
if self.key.scan(/^\+[1-9][0-9]\d{1,14}$/)[0] == self.key
puts "Your key is a valid Phone"
self.set_type("phone")
return "valid"
return self.set_key("invalid")
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论