ruby 使用 RSpec 测试哈希内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8392884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:33:55  来源:igfitidea点击:

Testing hash contents using RSpec

rubyrspecexpectations

提问by steve_gallagher

I have a test like so:

我有一个这样的测试:

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 

which, I know, is verbose. Is there a more concise way to state my expectations. I've looked at the docs but I can't see where to compress my expectations. Thanks.

我知道,这是冗长的。有没有更简洁的方式来表达我的期望。我看过文档,但我看不到在哪里压缩我的期望。谢谢。

回答by David Chelimsky

http://rubydoc.info/gems/rspec-expectations/RSpec/Matchers:include

http://rubydoc.info/gems/rspec-expectations/RSpec/Matchers:include

It works for hashes too:

它也适用于哈希:

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

回答by Benjamin Cheah

Just wanna add to @David's answer. You could nest and use matchers in your includehash. For example:

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

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

A caveat: a nested includehash must test all keys or the test will fail, e.g.:

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

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

回答by Marko Avlija?

Syntax has changed for RSpec 3, but include matcher is still the one:

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

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

See built-in-matchers#include-matcher.

请参阅内置匹配器#include-matcher

回答by Kiry Meas

Another easy way to test if the whole content is a Hash is to checkout if the content is the Hash Object itself:

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

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


For the question above we can check as follow:

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

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