【问题标题】:Testing hash contents using RSpec使用 RSpec 测试哈希内容
【发布时间】:2011-12-05 22:37:31
【问题描述】:

我有一个这样的测试:

it "should not indicate backwards jumps if the checker position is not a king" do
    board = Board.new
    game_board = board.create_test_board
    board.add_checker(game_board, :red, 3, 3)
    x_coord = 3
    y_coord = 3
    jump_locations = {}
    jump_locations["upper_left"]  = true 
    jump_locations["upper_right"] = false 
    jump_locations["lower_left"]  = false
    jump_locations["lower_right"] = true
    adjusted_jump_locations = @bs.adjust_jump_locations_if_not_king(game_board, x_coord, y_coord, jump_locations)
    adjusted_jump_locations["upper_left"].should == true 
    adjusted_jump_locations["upper_right"].should == false 
    adjusted_jump_locations["lower_left"].should == false
    adjusted_jump_locations["lower_right"].should == false
  end 

据我所知,这很冗长。有没有更简洁的方式来表达我的期望?我查看了文档,但看不到在哪里压缩我的期望。谢谢。

【问题讨论】:

    标签: ruby rspec expectations


    【解决方案1】:

    它也适用于哈希:

    expect(jump_locations).to include(
      "upper_left"  => true,
      "upper_right" => false,
      "lower_left"  => false,
      "lower_right" => true
    )
    

    来源: include matcher @ relishapp.com

    【讨论】:

    • 谢谢,大卫。顺便说一句,巨大的粉丝。真的很喜欢这本 RSpec 书。
    • 希望有类似match_array这样的对应方法
    • Fanage David 的同上!您的“Rspec 书”备受赞誉!
    【解决方案2】:

    只是想添加到@David 的答案。您可以在 include 哈希中嵌套和使用匹配器。例如:

    # Pass
    expect({
      "num" => 5, 
      "a" => { 
        "b" => [3, 4, 5] 
      }
    }).to include({
      "num" => a_value_between(3, 10), 
      "a" => {
        "b" => be_an(Array)
      }
    })
    

    警告:嵌套的 include 哈希必须测试所有键,否则测试将失败,例如:

    # Fail
    expect({
      "a" => { 
        "b" => 1,
        "c" => 2
      }
    }).to include({
      "a" => {
        "b" => 1
      }
    })
    

    【讨论】:

    • 您可以通过使用嵌套包含来解决您的警告:expect({ "a" => { "b" => 1, "c" => 2 } }).to include({ "a" => include({ "b" => 1 }) })
    • 大多数匹配器都有“动词”和更长的“名词”别名,后者在嵌套时可能会更好读:expect({ "a" => { "b" => 1, "c" => 2 } }).to include({ "a" => a_hash_including({ "b" => 1 }) })timjwade.com/2016/08/01/… 是一篇很好的博文。
    • @AngelCabo 您的评论应该是公认的答案。在很多情况下都有帮助。
    【解决方案3】:

    RSpec 3 的语法已更改,但包含匹配器仍然是一个:

    expect(jump_locations).to include(
      "upper_left" => true,
      "upper_right" => false,
      "lower_left" => false,
      "lower_right" => true
    )
    

    built-in-matchers#include-matcher

    【讨论】:

      【解决方案4】:

      另一种测试整个内容是否为哈希的简单方法是检查内容是否为哈希对象本身:

      it 'is to be a Hash Object' do
          workbook = {name: 'A', address: 'La'}
          expect(workbook.is_a?(Hash)).to be_truthy
      end
      

      对于上述问题,我们可以检查如下:

      expect(adjusted_jump_locations).to match(hash_including('upper_left' => true))
      

      【讨论】:

      • 问题是关于测试哈希内容,而不是响应是否是哈希
      • @ToTenMilan 这就是为什么我强调它有“另一种方式......”它可能/可能没有帮助。这取决于。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-25
      • 2016-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-29
      • 1970-01-01
      相关资源
      最近更新 更多