How to structure rspec data test?

Background

I'm relatively new to Ruby (and RSpec), and I have application code that emits a data object. I want my specification to verify that the hash structure is correct (i.e. contains all the necessary keys) and that the data is correct (i.e. that the inputs were correctly combined with the default values). Here's a trivialized example:

def returns_the_hash
  {
    :foo => "bar",
    :baz => "qux",
    :options => {
      :oof => "rab",
      :zab => "xuq",
    },
  }
end

In the future, it will be taken into account that the entire hash is determined based on the input data (which are available for my specification). The choice I have to make is how extended or compact to make my claims. Suppose that in this case I am only interested in pairs :fooand :options[:zab]:

Explicit Test

Advantage : better separation of intentions

: ;

it "should be structured properly" do
  hsh = returns_the_hash
  expect(hsh).to have_key(:foo)
  expect(hsh).to have_key(:options)
  expect(hsh[:options]).to have_key(:zab)
end

it "should merge inputs with default values" do
  hsh = returns_the_hash
  # assumes that the structure is correct
  expect(hsh[:foo]).to eq("bar")
  expect(hsh[:options][:zab]).to eq("xuq")
end

""

: ;

: ; /

it "should be well-formed" do
  hsh = returns_the_hash
  # also assumes that the structure is correct; doesn't verify it outright
  expect(hsh[:foo]).to eq("bar")
  expect(hsh[:options][:zab]).to eq("xuq")
end

, .

(, , ) rspec / , ? ( ) "" , , , "" ?

​​ "" Ruby/Rspec?

+3
1

returns_the_hash

{
  :lorem => "ipsum",
  :foo => "bar",
  :baz => "qux",
  :options => {
    :oof => "rab",
    :zab => "xuq",
  },
}

, lorem/ipsum, ?

, , exact .

, , , , .

, . , , , ( .)

0

All Articles