2011-12-05 17 views
42

Ho un test in questo modo:Testing contenuti hash utilizzando RSpec

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 

che, lo so, è prolisso. C'è un modo più conciso per esprimere le mie aspettative. Ho esaminato i documenti ma non riesco a vedere dove comprimere le mie aspettative. Grazie.

risposta

78

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

Si lavora per hash troppo:

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

Grazie, David. BTW Enorme fan. Mi è piaciuto molto il libro di RSpec. –

+0

Vorrei che ci fosse un metodo corrispondente come match_array –

+0

Idem sul Fanage David! Il tuo "The Rspec Book" è ben adattato! –

18

Voglio solo aggiungere ai @ risposta di David. È possibile nidificare e utilizzare gli abbinamenti nell'hash include. Ad esempio:

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

Un avvertimento: nested include hash deve testare tutte le chiavi o il test fallisce, ad esempio:

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

è possibile risolvere i caveat da utilizzando nidificato include: '' 'aspettarsi ({ "a"=> { "b"=> 1, "c"=> 2 } }) per includere (. { "a" => include ({ "b" => 1 })}) '' ' – AngelCabo

+0

la maggior parte dei matcher ha sia alias" verb "che" noun "più lunghi, quest'ultimo potrebbe leggere meglio durante l'annidamento:' expect ({"a" => {"b" => 1, "c" => 2}}). per includere ({"a" => a_hash_including ({"b" => 1})}) '. http://timjwade.com/2016/08/01/testing-json-apis-with-rspec-composable-matchers.html è un buon post sul blog su questo. –

2

sintassi è cambiato per RSpec 3, ma includono matcher è ancora quello:

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

Vedere built-in-matchers#include-matcher.

0

Un altro modo semplice per verificare se tutto il contenuto è un hash è alla cassa, se il contenuto è l'oggetto stesso Hash:

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