RSpec: do not verify hash keys separately

Sometimes I come across three separate checks in specs to make sure the hash is OK: there are keys, there are values, values have the right type.

RSpec.describe UserSerializer do
describe “#as_json” do
let(:user) do
build(:user,


This content originally appeared on DEV Community and was authored by Vasily Polovnyov

Sometimes I come across three separate checks in specs to make sure the hash is OK: there are keys, there are values, values have the right type.

RSpec.describe UserSerializer do
  describe "#as_json" do
    let(:user) do
      build(:user,
        first_name: "Bart",
        last_name: "Simpson",
        tel: "+777123")
    end
    let(:json) { described_class.new(user).as_json }

    it "has keys" do
      expect(json).to include(:first_name, :last_name, :tel)
    end

    it "has types" do
      expect(json[:first_name]).to be_kind_of(String)
      expect(json[:last_name]).to be_kind_of(String)
      expect(json[:tel]).to be_kind_of(String)
    end

    it "has values" do
      expect(json[:first_name]).to eq(user.first_name)
      expect(json[:last_name]).to eq(user.last_name)
      expect(json[:tel]).to eq(user.tel)
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

This spec is more confusing than helpful: when you change fields, you have to update three tests; it's hard to immediately understand what the serializer returns.

Better to use include matcher and skip type checks (it's not the responsibility of the test):

RSpec.describe UserSerializer do
  describe "#as_json" do
    it "includes first name, last name and tel" do
      user = build(:user,
        first_name: "Bart",
        last_name: "Simpson",
        tel: "+777123")
      json = described_class.new(user).as_json

      expect(json).to include(
        first_name: "Bart",
        last_name: "Simpson",
        tel: "+777123"
      )
    end
  end
end
Enter fullscreen mode Exit fullscreen mode


This content originally appeared on DEV Community and was authored by Vasily Polovnyov


Print Share Comment Cite Upload Translate Updates
APA

Vasily Polovnyov | Sciencx (2021-03-02T10:03:42+00:00) RSpec: do not verify hash keys separately. Retrieved from https://www.scien.cx/2021/03/02/rspec-do-not-verify-hash-keys-separately/

MLA
" » RSpec: do not verify hash keys separately." Vasily Polovnyov | Sciencx - Tuesday March 2, 2021, https://www.scien.cx/2021/03/02/rspec-do-not-verify-hash-keys-separately/
HARVARD
Vasily Polovnyov | Sciencx Tuesday March 2, 2021 » RSpec: do not verify hash keys separately., viewed ,<https://www.scien.cx/2021/03/02/rspec-do-not-verify-hash-keys-separately/>
VANCOUVER
Vasily Polovnyov | Sciencx - » RSpec: do not verify hash keys separately. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/02/rspec-do-not-verify-hash-keys-separately/
CHICAGO
" » RSpec: do not verify hash keys separately." Vasily Polovnyov | Sciencx - Accessed . https://www.scien.cx/2021/03/02/rspec-do-not-verify-hash-keys-separately/
IEEE
" » RSpec: do not verify hash keys separately." Vasily Polovnyov | Sciencx [Online]. Available: https://www.scien.cx/2021/03/02/rspec-do-not-verify-hash-keys-separately/. [Accessed: ]
rf:citation
» RSpec: do not verify hash keys separately | Vasily Polovnyov | Sciencx | https://www.scien.cx/2021/03/02/rspec-do-not-verify-hash-keys-separately/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.