File: have_attributes.feature

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (46 lines) | stat: -rw-r--r-- 1,996 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Feature: `have_attributes` matcher

  Use the have_attributes matcher to specify that an object's attributes match the expected attributes:

  ```ruby
    Person = Struct.new(:name, :age)
    person = Person.new("Jim", 32)

    expect(person).to have_attributes(:name => "Jim", :age => 32)
    expect(person).to have_attributes(:name => a_string_starting_with("J"), :age => (a_value > 30) )
  ```

  The matcher will fail if actual doesn't respond to any of the expected attributes:

  ```ruby
    expect(person).to have_attributes(:name => "Jim", :color => 'red')
  ```

  Scenario: Basic usage
    Given a file named "basic_have_attributes_matcher_spec.rb" with:
      """ruby
      Person = Struct.new(:name, :age)

      RSpec.describe Person.new("Jim", 32) do
        it { is_expected.to have_attributes(:name => "Jim") }
        it { is_expected.to have_attributes(:name => a_string_starting_with("J") ) }
        it { is_expected.to have_attributes(:age => 32) }
        it { is_expected.to have_attributes(:age => (a_value > 30) ) }
        it { is_expected.to have_attributes(:name => "Jim", :age => 32) }
        it { is_expected.to have_attributes(:name => a_string_starting_with("J"), :age => (a_value > 30) ) }
        it { is_expected.not_to have_attributes(:name => "Bob") }
        it { is_expected.not_to have_attributes(:age => 10) }
        it { is_expected.not_to have_attributes(:age => (a_value < 30) ) }

        # deliberate failures
        it { is_expected.to have_attributes(:name => "Bob") }
        it { is_expected.to have_attributes(:age => 10) }

        # fails if any of the attributes don't match
        it { is_expected.to have_attributes(:name => "Bob", :age => 32) }
        it { is_expected.to have_attributes(:name => "Jim", :age => 10) }
        it { is_expected.to have_attributes(:name => "Bob", :age => 10) }
      end
      """
    When I run `rspec basic_have_attributes_matcher_spec.rb`
    Then the output should contain "14 examples, 5 failures"