Here's a concise example
describe MyClass do
describe "#update" do
it "appends the argument to the array" do
subject.update(value = 'some_value')
expect(subject.ary).to eq([value])
end
end
end
A few notes compared to your initial code. First of all, you can rely on subject. subject is defined as
described_class.new
It's like having
describe MyClass do
subject { described_class.new }
describe "#update" do
...
end
end
or
describe MyClass do
describe "#update" do
subject = described_class.new
end
end
which essentially means
describe MyClass do
describe "#update" do
subject = MyClass.new
end
end
If you just want to test an instance, use subject. Do not use before actions to set instance variables.
The following line
subject.update(value = 'some_value')
expect(subject.ary).to eq([value])
is essentially a shortcut for
value = 'some_value'
subject.update(value)
expect(subject.ary).to eq([value])
It helps me to not write the expected string twice. The rest should be quite easy to understand.
You may also want to test the return value, if this is relevant.
describe MyClass do
describe "#update" do
it "appends the argument to the array" do
subject.update(value = 'some_value')
expect(subject.ary).to eq([value])
end
it "returns the item that was added" do
value = 'some_value'
expect(subject.update(value)).to eq(value)
end
end
end
before {@my_class.update('some_value'}afterdescribe '#update' do, then useexpect(@my_class.ary).to eq(['some_value'])?