Ruby RSPEC测试
我想知道如何满足is_valid
测试,我以多种方式尝试
require 'spec_helper'
RSpec.describe PixKey do
subject(:pix_key) { described_class.new(key) }
let(:key) { '[email protected]' }
describe '.new' do
context 'with a valid key' do
it { is_expected.to be_a(described_class) }
it { is_expected.to be_valid }
end
它
class PixKey
attr_accessor :key
def new(key)
@key = :key
validates :key, presence: true
end
end
了
pixkey .new 使用有效的键 预计将是一种pixkey
is expected to be valid (FAILED - 1)
I would like to know how to satisfy the is_valid
test, I tried it in several ways but the test does not pass what I have to write in the class
require 'spec_helper'
RSpec.describe PixKey do
subject(:pix_key) { described_class.new(key) }
let(:key) { '[email protected]' }
describe '.new' do
context 'with a valid key' do
it { is_expected.to be_a(described_class) }
it { is_expected.to be_valid }
end
Cod class
class PixKey
attr_accessor :key
def new(key)
@key = :key
validates :key, presence: true
end
end
Err
PixKey .new
with a valid key
is expected to be a kind of PixKey
is expected to be valid (FAILED - 1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是一个测试驱动的开发(TDD)分配,您可以获得一些规格,并且必须实现通过测试的类。
为了获得绿色规格,您必须实现一些方法。为此,您必须了解RSPEC的语法:
Dricesd
之后发生的任何事情。在您的规格中,它是描述pixkey
,因此Dictical_class.new(key)
等于pixkey.new(key
有效?
让我们从一个空的类开始:
运行规格给出一个错误:(
- fail-fast-fail-fast
使RSPEC在第一个失败规格之后停止RSPEC)RSPEC称为
pixkey.new(key)
但new
不进行参数。为了使该工作工作,您 可以实现一种称为new
的类方法,即:但是Ruby类已经带有一个内置的
new 方法可以处理处理该方法为您分配。通常,您只需实现一个称为
初始化
的实例方法:Ruby的内置
new
将调用initialize
inationalize ,然后将所有参数传递给它。请注意,我对
键
尚无任何操作。这是TDD的核心部分:您的写作足以获得测试通过。让我们再试一次:这次,它抱怨我们的
pixkey
实例不响应有效吗?
,让我们实现该方法: >是方法名称的一部分)并再次运行测试:
现在说我们的
有效?
方法有望返回“真实”结果。在Ruby中,除了nil
和false
之外,所有内容都符合真实的条件,但我只是只使用true
:在这一点上,规格通过。但是显然,该类远非完整:
键
从未使用过,有效?
只是总是返回true
而无需验证任何内容。我认为还有更多的测试可以通过。继续以小步骤工作,一次只进行一次测试。
I assume that this is a test driven development (TDD) assignment where you got some specs and have to implement a class that passes the tests.
In order to get your specs green, you have to implement some methods. To do so, you have to understand RSpec's syntax:
described_class
– this is whatever comes afterdescribe
. In your spec, it'sdescribe PixKey
, sodescribed_class.new(key)
is equivalent toPixKey.new(key)
be_a
is a type matcher and equivalent to callingkind_of?
be_valid
is a predicate matcher and equivalent to callingvalid?
Let's start with an empty class:
Running the specs gives an error: (
--fail-fast
makes RSpec stop after the first failing spec)RSpec called
PixKey.new(key)
butnew
doesn't take an argument. In order to get that working, you could implement a class method callednew
, i.e.:But Ruby classes already come with a built-in
new
method that handles the allocation for you. You typically just implement an instance method calledinitialize
:Ruby's built-in
new
will callinitialize
and pass all arguments to it.Note that I don't do anything with
key
yet. This is a core part of TDD: you write just enough to get the test passing. Let's try it again:This time, it's complaining about our
PixKey
instance not responding tovalid?
, so let's implement that method: (the trailing?
is part of the method name)And run the tests again:
Now it says that our
valid?
method is expected to return a "truthy" result. In Ruby, everything besidesnil
andfalse
qualifies as truthy, but I'd just just usetrue
:At this point, the specs pass. But obviously, the class is far from complete:
key
is never used andvalid?
just always returnstrue
without validating anything.I assume that there are more tests to pass. Keep working in small steps, just one test at a time.