Class: TurboRspec::Matchers::MatchTurboStreamSnapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/turbo_rspec/matchers/match_turbo_stream_snapshot.rb

Overview

RSpec matcher that records a turbo stream response on the first run and diffs against the stored snapshot on subsequent runs.

Snapshots are written to the directory configured by +TurboRspec.configuration.snapshot_dir+ (default: +spec/snapshots/turbo+). Each snapshot is stored as +name.turbo+.

Set +UPDATE_TURBO_SNAPSHOTS=1+ to overwrite existing snapshots.

Examples:

expect(response).to match_turbo_stream_snapshot("messages/new")

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ MatchTurboStreamSnapshot

Returns a new instance of MatchTurboStreamSnapshot.



19
20
21
# File 'lib/turbo_rspec/matchers/match_turbo_stream_snapshot.rb', line 19

def initialize(name)
  @name = name
end

Instance Method Details

#descriptionString

Returns:

  • (String)


58
59
60
# File 'lib/turbo_rspec/matchers/match_turbo_stream_snapshot.rb', line 58

def description
  "match turbo stream snapshot #{@name.inspect}"
end

#does_not_match?(response_or_body) ⇒ Boolean

Parameters:

  • response_or_body (#body, String)

Returns:

  • (Boolean)


40
41
42
# File 'lib/turbo_rspec/matchers/match_turbo_stream_snapshot.rb', line 40

def does_not_match?(response_or_body)
  !matches?(response_or_body)
end

#failure_messageString

Returns:

  • (String)


45
46
47
48
49
50
# File 'lib/turbo_rspec/matchers/match_turbo_stream_snapshot.rb', line 45

def failure_message
  "expected response to match turbo stream snapshot #{@name.inspect}\n\n" \
    "stored:\n#{@stored}\n\n" \
    "actual:\n#{@actual}\n\n" \
    "To update: run with UPDATE_TURBO_SNAPSHOTS=1"
end

#failure_message_when_negatedString

Returns:

  • (String)


53
54
55
# File 'lib/turbo_rspec/matchers/match_turbo_stream_snapshot.rb', line 53

def failure_message_when_negated
  "expected response not to match turbo stream snapshot #{@name.inspect}"
end

#matches?(response_or_body) ⇒ Boolean

Parameters:

  • response_or_body (#body, String)

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/turbo_rspec/matchers/match_turbo_stream_snapshot.rb', line 25

def matches?(response_or_body)
  @actual = extract_body(response_or_body)
  @path = snapshot_path

  if update_snapshots? || !File.exist?(@path)
    write_snapshot(@actual)
    true
  else
    @stored = File.read(@path)
    @actual.strip == @stored.strip
  end
end