RSpec::Expectations::ExpectationNotMetError: expected X got Y Ruby / Rails Origin: Application & API OSI layer 7 · Application Last updated 5 Nov 2026

RSpec: expected: 200 got: 404 (compared using ==) — RSpec::Expectations::ExpectationNotMetError

An RSpec expect assertion didn't hold: the actual value differs from what the test expected.

Exact message you may see RSpec::Expectations::ExpectationNotMetError: expected: 200 got: 404 (compared using ==)expected: 200 got: 404expected #<User> to eq #<User>Failure/Error: expect(response).to have_http_status(:ok)Failure/Error: expect(response.status).to eq(200)
Seen on: Ruby

Meaning

Read "expected" vs "got" and the diff. Typical in request specs: wrong status because of auth/fixtures/factories, missing login_as, or changed JSON structure. Debug with puts response.body, use aggregate_failures, check let vs let! laziness, and DB cleaning/transactions.

Common causes

  • Wrong status/body due to missing setup (auth, factories)
  • Lazy let not created before the request
  • Data leaking between tests (no cleaning)
  • Time/random dependent code
  • Behaviour changed after refactor

⚡ Quick fix

  1. Print response.body/pp record to see the actual state
  2. Use let! or before to create data
  3. Use freeze_time/travel_to for time-based tests

Detailed fix by platform

Ruby

  1. bundle exec rspec spec/requests/users_spec.rb:12 --format documentation

How to diagnose

  1. Diff — Expected vs got?
  2. Setup — Data created?
  3. Isolation — Clean DB?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

ActiveRecord::RecordInvalid: Validation failed — Rails: ActiveRecord::RecordInvalid: Validation failed: Email can't be blank, Name is too short (Ruby / Rails)ActiveRecord::RecordI…ArgumentError: missing keyword / unknown keyword — Ruby: ArgumentError: missing keyword: :name / unknown keyword: :nmae (Ruby / Rails)ArgumentError: missin…ArgumentError: wrong number of arguments (given 1, expected 2) — Ruby: ArgumentError: wrong number of arguments (given 1, expected 2..3) (Ruby / Rails)ArgumentError: wrong …Bundler could not find compatible versions for gem — Bundler: Bundler could not find compatible versions for gem "activesupport": In Gemfile: rails (= 7.1.0) depends on activesupport (= 7.1.0) (Ruby / Rails)Bundler could not fin…Bundler: Could not find gem in locally installed gems — Bundler: Could not find gem 'rails (~> 7.1)' in locally installed gems / Could not find rails-7.1.0 in any of the gem sources listed in your Gemfile (Ruby / Rails)Bundler: Could not fi…FactoryBot::DuplicateDefinitionError: Factory already registered — FactoryBot: FactoryBot::DuplicateDefinitionError: Factory already registered: user (Ruby / Rails)FactoryBot::Duplicate…rake aborted! Don't know how to build task — Rake: rake aborted! Don't know how to build task 'db:migrat' (See the list of available tasks with `rake --tasks`) (Ruby / Rails)rake aborted! Don't k…RSpec::Ex…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.