[英]Ruby RSPEC test
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我想知道如何满足is_valid
测试,我尝试了几种方法,但测试没有通过我必须在课堂上写的内容
require 'spec_helper'
RSpec.describe PixKey do
subject(:pix_key) { described_class.new(key) }
let(:key) { 'email@wesley.io' }
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具有有效密钥的 PixKey .new 预计是一种 PixKey
is expected to be valid (FAILED - 1)
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.我假设这是一个测试驱动开发 (TDD) 任务,您在其中获得了一些规范,并且必须实现一个通过测试的类。
In order to get your specs green, you have to implement some methods.为了使您的规范绿色,您必须实现一些方法。 To do so, you have to understand RSpec's syntax:为此,您必须了解 RSpec 的语法:
described_class
– this is whatever comes after describe
. described_class
- 这是describe
之后的任何内容。 In your spec, it's describe PixKey
, so described_class.new(key)
is equivalent to PixKey.new(key)
在您的规范中,它是describe PixKey
,所以described_class.new(key)
等效于PixKey.new(key)
be_a
is a type matcher and equivalent to calling kind_of?
be_a
是一个类型匹配器,相当于调用kind_of?
be_valid
is a predicate matcher and equivalent to calling valid?
be_valid
是一个谓词匹配器,相当于调用valid?
Let's start with an empty class:让我们从一个空类开始:
class PixKey
end
Running the specs gives an error: ( --fail-fast
makes RSpec stop after the first failing spec)运行规范会出错:( --fail-fast
使 RSpec 在第一个失败的规范后停止)
$ rspec pix_key_spec.rb --fail-fast
Failures:
1) PixKey.new with a valid key
Failure/Error: subject(:pix_key) { described_class.new(key) }
ArgumentError:
wrong number of arguments (given 1, expected 0)
❌ 1 example, 1 failure
RSpec called PixKey.new(key)
but new
doesn't take an argument. RSpec 调用PixKey.new(key)
但new
不带参数。 In order to get that working, you could implement a class method called new
, ie:为了让它工作,你可以实现一个名为new
的类方法,即:
class PixKey
def self.new(key)
allocate
end
end
But Ruby classes already come with a built-in new
method that handles the allocation for you.但是 Ruby 类已经带有一个为您处理分配的内置new
方法。 You typically just implement an instance method called initialize
:您通常只需实现一个名为initialize
的实例方法:
class PixKey
def initialize(key)
end
end
Ruby's built-in new
will call initialize
and pass all arguments to it. Ruby 的内置new
将调用initialize
并将所有参数传递给它。
Note that I don't do anything with key
yet.请注意,我还没有对key
做任何事情。 This is a core part of TDD: you write just enough to get the test passing.这是 TDD 的核心部分:您编写的代码足以让测试通过。 Let's try it again:让我们再试一次:
$ rspec pix_key_spec.rb --fail-fast
Failures:
1) PixKey.new with a valid key is expected to be valid
Failure/Error: it { is_expected.to be_valid }
expected #<PixKey:0x000000010201de90> to respond to `valid?`
❌ 2 examples, 1 failure
This time, it's complaining about our PixKey
instance not responding to valid?
这一次,它抱怨我们的PixKey
实例没有响应valid?
, so let's implement that method: (the trailing ?
is part of the method name) ,所以让我们实现该方法:(尾随?
是方法名称的一部分)
class PixKey
def initialize(key)
end
def valid?
end
end
And run the tests again:并再次运行测试:
$ rspec pix_key_spec.rb --fail-fast
Failures:
1) PixKey.new with a valid key is expected to be valid
Failure/Error: it { is_expected.to be_valid }
expected `#<PixKey:0x00000001027b1da0>.valid?` to be truthy, got nil
❌ 2 examples, 1 failure
Now it says that our valid?
现在它说我们的valid?
method is expected to return a "truthy" result.方法预计会返回一个“真实”的结果。 In Ruby, everything besides nil
and false
qualifies as truthy, but I'd just just use true
:在 Ruby 中,除了nil
和false
之外的所有内容都被认为是真实的,但我只使用true
:
class PixKey
def initialize(key)
end
def valid?
true
end
end
$ rspec pix_key_spec.rb --fail-fast
✅ 2 examples, 0 failures
At this point, the specs pass.至此,规范通过了。 But obviously, the class is far from complete: key
is never used and valid?
但显然,这个类还远未完成: key
从未使用过且valid?
just always returns true
without validating anything.总是返回true
而不验证任何内容。
I assume that there are more tests to pass.我认为还有更多的测试要通过。 Keep working in small steps, just one test at a time.继续以小步骤工作,一次只进行一项测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.