DEV Community

Vasily Polovnyov
Vasily Polovnyov

Posted on

2 2

RSpec: what's important for the test

Situation: we need to make sure that we charge the user the correct amount when he uses a coupon. Important point: the class responsible for this has a huge API with mandatory arguments.

First take:

context "when coupon is applied" do
  it "charges user $500" do
    allow(Cashier).to receive(:charge)

    purchase.perform

    expect(Cashier).to have_received(:charge)
      .with(
        user: user,
        sum: 500,
        description: "season ticket purchase",
        coupon: coupon
      )
  end
end
Enter fullscreen mode Exit fullscreen mode

If we leave the test as it is, it will distract us with details that are not important: the user, the description of the charge, the coupon. Since we want to make sure that the amount has changed, we only need to check the amount:

context "when coupon is applied" do
  context "charges user $500" do
    allow(Cashier).to receive(:charge)

    purchase.perform

    expect(Cashier).to have_received(:charge)
      .with(hash_including(sum: 500))
  end
end
Enter fullscreen mode Exit fullscreen mode

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →