File: rspec_set_spec.rb

package info (click to toggle)
ruby-rspec-set 0.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 128 kB
  • sloc: ruby: 246; makefile: 4
file content (165 lines) | stat: -rw-r--r-- 3,929 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
require 'spec_helper'

describe 'including Set' do
  it 'adds the ::set method to RSpec::Core::ExampleGroup' do
    expect(RSpec::Core::ExampleGroup).to respond_to(:set)
  end
end

describe 'without an ActiveRecord model' do
  setup_for_error_checking(NonActiveRecordClass)

  it "warns the user that Set only works with AR models" do
    $stderr.rewind
    expect($stderr.string.chomp).to eq(
      "my_object is a NonActiveRecordClass - rspec-set works with ActiveRecord models only"
    )
  end
end

describe 'with an ActiveRecord model' do
  setup_for_error_checking(ActiveRecordClassExample)

  it "doesn't give a warning to the user" do
    $stderr.rewind
    expect($stderr.string.chomp).to be_empty
  end

  it 'creates a method based on the argument to ::set' do
    expect(self).to respond_to(:my_object)
  end
end

describe 'with a destroyed ActiveRecord model' do
  set(:my_destroyed_object) do
    ActiveRecordClassExample.create(name: 'Alfred', age: 77)
  end

  it 'allows us to destroy a model' do
    my_destroyed_object.destroy
    expect(
      ActiveRecordClassExample.find_by(id: my_destroyed_object.id)
    ).to be_nil
  end

  it 'reloads a destroyed model' do
    expect(my_destroyed_object.reload.name).to eq('Alfred')
  end
end

describe 'with a stale model' do
  set(:my_stale_object) do
    ActiveRecordClassExample.create(name: 'Old Name', age: 18)
  end

  it 'allows us to play with the model' do
    my_stale_object.update(name: 'New Name')
    expect(ActiveRecordClassExample.find(my_stale_object.id).name).to eq(
      'New Name'
    )
  end

  it 'reloads the stale model' do
    expect(my_stale_object.name).to eq('Old Name')
  end
end


describe ActiveRecordClassExample do
  set(:ar_class_example) { ActiveRecordClassExample.create(name: 'ex_1') }

  subject { ar_class_example }

  context "when name is changed to 'ex_2" do
    before do
      ar_class_example.update(name: 'ex_2')
    end

    it 'updates the name' do
      expect(subject.name).to eq('ex_2')
    end
  end

  context "when name is 'ex_1" do
    it 'reloads the original name' do
      expect(subject.name).to eq('ex_1')
    end
  end
end

describe 'sub sub contexts' do
  set(:ar_class_example) { ActiveRecordClassExample.create(name: 'apple') }

  subject { ar_class_example }

  context "when name is changed to 'banana'" do
    before do
      ar_class_example.update(name: 'banana')
    end

    it 'updates the name' do
      expect(subject.name).to eq('banana')
    end

    context "when we append ' is good'" do
      before do
        ar_class_example.name << ' is good'
        ar_class_example.save
      end

      it 'updates the appended name' do
        expect(subject.name).to eq('banana is good')
      end
    end

    context "when we append ' is bad'" do
      before do
        ar_class_example.name << ' is bad'
        ar_class_example.save
      end

      it 'also updates the appended name' do
        expect(subject.name).to eq('banana is bad')
      end

      context "when we append ' for you'" do
        before do
          ar_class_example.name << ' for you'
          ar_class_example.save
        end

        it 'contains the full sentence' do
          expect(subject.name).to eq('banana is bad for you')
        end
      end
    end
  end

  context "when name is 'apple'" do
    it 'reloads the original name' do
      expect(subject.name).to eq('apple')
    end
  end
end

describe 'deleting an object' do
  set(:ar_class_example) { ActiveRecordClassExample.create(name: 'apple') }

  subject { ar_class_example }

  context "when I destroy the ar_class_example" do
    before do
      ar_class_example.destroy
    end

    it "is destroyed" do
      expect(ActiveRecordClassExample.find_by_id(ar_class_example.id)).to be_nil
    end
  end

  context "when name is 'apple'" do
    it 'is reloaded from the database' do
      expect(subject.name).to eq('apple')
    end
  end
end