rspec的“expect”可以解析一个块来确认嵌套数组/网格吗?

时间:2016-09-18 09:21:07

标签: ruby rspec

如何使用rspecs expect语法检查嵌套数组?

此代码块使用rspecs should语法:

subject.cell_grid.each do |row|
  row.is_a?(Array).should be_true
end

......我想我的第22行和第22行语法正确。 23,“spec_game_of_life.rb”文件,但是,当我用rspec检查文件时,我收到以下错误:

user@ubuntu:~/Ruby/GameOfLife$ rspec spec_game_of_life.rb
..F

Failures:

  1) Game of Life world should create proper cell_grid upon initialization
     Failure/Error:
       expect(subject.cell_grid.each) do |row|
         row.is_a?(Array).to be true
       end

     ArgumentError:
       You cannot pass both an argument and a block to `expect`.
     # ./spec_game_of_life.rb:22:in `block (3 levels) in <top (required)>'

Finished in 0.0019 seconds (files took 0.11777 seconds to load)
3 examples, 1 failure

Failed examples:

rspec ./spec_game_of_life.rb:19 # Game of Life world should create proper cell_grid upon initialization

据我所知,rspec的“应该”已被“expect”替换为:http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/

使用(rows,cols)单元格网格初始化一个类 - Ruby脚本,“game_of_life.rb”:

  1 # basic file
  2 
  3 class World
  4   attr_accessor :rows, :cols, :cell_grid
  5   def initialize(rows=3, cols=3)
  6     @rows      = rows
  7     @cols      = cols
  8     @cell_grid = Array.new(rows) do |row|
  9                    Array.new(cols) do |col|
 10                    end
 11                  end
 12   end
 13 end

Ruby spec文件,“spec_game_of_life.rb”:

  1 # spec file
  2 
  3 require 'rspec'
  4 require_relative 'game_of_life.rb'
  5 
  6 describe 'Game of Life' do
  7 
  8   context 'world' do
  9     subject { World.new }
 10 
 11     it 'should create a new world object' do
 12       expect(subject.is_a?(World)).to be true
 13     end
 14     it 'should respond to proper methods' do
 15       expect(subject.respond_to?(:rows))
 16       expect(subject.respond_to?(:cols))
 17       expect(subject.respond_to?(:cell_grid))
 18     end
 19     it 'should create proper cell_grid upon initialization' do
 20       expect(subject.cell_grid.is_a?(Array)).to be true
 21 
 22       expect(subject.cell_grid.each) do |row|
 23         row.is_a?(Array).to be true
 24       end
 25     end
 26 
 27   end
 28 
 29 end

FWIW:Ubuntu 14.04,Ruby 2.3.0,rspec 3.5.3&amp;我正在关注使用“应该”的“生命游戏”教程: https://www.youtube.com/watch?v=Tzs3_pl410M&list=PLMC91Ry9EhRKUn0MIdgXrZiptF7nVyYoQ&index=4

来自Bartek Gladys的每个回答的编辑:

期待{subject.cell_grid.all? {| k | k.is_a?(Array)}} .to eq true

..F

Failures:

  1) Game of Life world should create proper cell_grid upon initialization
     Failure/Error: expect{ subject.cell_grid.all? { |k| k.is_a?(Array) } }.to eq true
       You must pass an argument rather than a block to use the provided matcher (eq true), or the matcher must implement `supports_block_expectations?`.
     # ./spec_game_of_life.rb:22:in `block (3 levels) in <top (required)>'

注:

期待{subject.cell_grid.all? {| k | k.is_a?(Array)}} .to be true

 Failure/Error: expect{ subject.cell_grid.all? { |k| k.is_a?(Array) } }.to be true
   You must pass an argument rather than a block to use the provided matcher (equal true), or the matcher must implement `supports_block_expectations?`.

那么......如何实施supports_block_expectations?
研究人员:http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/
https://www.relishapp.com/rspec/rspec-expectations/docs/custom-matchers/define-a-matcher-supporting-block-expectations
http://www.relishapp.com/rspec/rspec-expectations/v/3-5/docs
http://rspec.info/

3 个答案:

答案 0 :(得分:1)

试试这个:

expect{ subject.cell_grid.all? { |k| k.is_a?(Array) } }.to eq true

答案 1 :(得分:1)

您的规范的预期版本是

subject.cell_grid.each do |row|
  expect(row.is_a?(Array)).to be_truthy
end

be_true匹配器不再存在)

稍微更自然地你会写

subject.cell_grid.each do |row|
  expect(row).to be_an(Array)
end

您可以使用all?只能进行一次预期调用,但这会导致失败消息的帮助减少。

你通常不会通过一个块来期望只是对一个值进行断言 - 通常这用于检查副作用(例如引发异常)。

答案 2 :(得分:0)

并非完全是答案,但是当我努力寻找如何定义 new Plan().stages( IntStream.iterate(1, i -> i < 5, i -> i + 1) .mapToObj(i -> new Stage("Stage " + i).jobs(commonJob)) .toArray() ); 时,它在下面:

说,我们要为匹配器supports_block_expectations提供块支持(可以是beeq甚至是您自己的匹配器)

注意:使用现有的匹配器可以重新定义它。最好用一些新名称来命名,以免造成麻烦。

定义

短手定义:

equal

完整定义

RSpec::Matchers.define :be do
  match do |actual|
    actual.is_a? Proc
  end

  supports_block_expectations
end

用法

RSpec::Matchers.define :equal do
  match do |actual|
    actual.is_a? Proc
  end

  def supports_block_expectations?
    true # or some logic
  end
end