Ruby RSPEC测试

发布于 2025-02-13 01:39:08 字数 764 浏览 0 评论 0原文

我想知道如何满足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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心安伴我暖 2025-02-20 01:39:08

我认为这是一个测试驱动的开发(TDD)分配,您可以获得一些规格,并且必须实现通过测试的类。

为了获得绿色规格,您必须实现一些方法。为此,您必须了解RSPEC的语法:

  • - 这是Dricesd之后发生的任何事情。在您的规格中,它是描述pixkey,因此Dictical_class.new(key)等于pixkey.new(key
  • ) /代码>是 type matcher 打电话
  • 代码> be_valid 是要调用有效?

让我们从一个空的类开始:

class PixKey
end

运行规格给出一个错误:( - fail-fast-fail-fast 使RSPEC在第一个失败规格之后停止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称为pixkey.new(key)new不进行参数。为了使该工作工作,您 可以实现一种称为new的类方法,即:

class PixKey
  def self.new(key)
    allocate
  end
end

但是Ruby类已经带有一个内置的 new 方法可以处理处理该方法为您分配。通常,您只需实现一个称为初始化的实例方法:

class PixKey
  def initialize(key)
  end
end

Ruby的内置new将调用initialize inationalize ,然后将所有参数传递给它。

请注意,我对尚无任何操作。这是TDD的核心部分:您的写作足以获得测试通过。让我们再试一次:

$ 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

这次,它抱怨我们的pixkey实例不响应有效吗?,让我们实现该方法: >是方法名称的一部分)

class PixKey
  def initialize(key)
  end

  def valid?
  end
end

并再次运行测试:

$ 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

现在说我们的有效?方法有望返回“真实”结果。在Ruby中,除了nilfalse之外,所有内容都符合真实的条件,但我只是只使用true

class PixKey
  def initialize(key)
  end

  def valid?
    true
  end
end
$ rspec pix_key_spec.rb --fail-fast

✅ 2 examples, 0 failures

在这一点上,规格通过。但是显然,该类远非完整:从未使用过,有效?只是总是返回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 after describe. In your spec, it's describe PixKey, so described_class.new(key) is equivalent to PixKey.new(key)
  • be_a is a type matcher and equivalent to calling kind_of?
  • be_valid is a predicate matcher and equivalent to calling 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)

$ 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. In order to get that working, you could implement a class method called new, i.e.:

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. You typically just implement an instance method called initialize:

class PixKey
  def initialize(key)
  end
end

Ruby's built-in new will call initialize 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:

$ 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?, 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? 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:

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? just always returns true without validating anything.

I assume that there are more tests to pass. Keep working in small steps, just one test at a time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文