The 5 Rules of Simple RSpec Tests | solnic.codes - 2 Top-down let definitions

PHOTO EMBED

Fri May 28 2021 14:38:37 GMT+0000 (UTC)

Saved by @captm

RSpec.describe CreateUser do
  subject(:create_user) do
    CreateUser.new
  end

  context "with valid params" do
    it "returns success" do
      expect(create_user.(name: "Jane", email: "jane@doe.org")).to be_success
    end
  end

  context "with invalid params" do
    # THIS RIGHT HERE - BAD, VERY BAD `LET`
    let(:create_user) do
      CreateUser.new(some_custom: "stuff for this example")
    end

    it "returns failure when name is missing" do
      expect(create_user.(name: "", email: "jane@doe.org")).to be_failure
    end

    it "returns failure when email is missing" do
      expect(create_user.(name: "Jane", email: "")).to be_failure
    end
  end
end
content_copyCOPY

https://solnic.codes/2021/05/11/the-5-rules-of-simple-rspec-tests/