secret
Last active

  • Download Gist
aaaa.md
Markdown

Assumptions

  • Assume there are functional specs as well, but they don't need to be changed to add this feature. Marking an invoice as paid is a PUT /invoices/123 with data { 'invoice' => { 'state' => 'paid' } }.
  • The view has been modified to display the "paid" badge, which uses invoice.paid?.

Thoughts

The main issue with this example, to me, is excessive unit testing. Is it worth the time to test Invoice#paid? in this manner? Requiring such test coverage can lead to laziness to avoid tests--you could do this by writing invoice.state == 'paid' in the view. The tests are also closely tied to the implementation (since the implementation is so simple).

invoice.rb
Ruby
1 2 3 4 5
class Invoice < ActiveRecord::Base
def paid?
state == 'paid'
end
end
invoice_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
describe Invoice
# normally would use factory_girl here, actually.
let(:invoice) { Invoice.new(:state => state) }
let(:state) { 'unpaid' }
 
describe '#paid?' do
subject { invoice.paid? }
context 'when state is paid' do
let(:state) { 'paid' }
it { should be_true }
end
context 'when state equals other values' do
let(:state) { 'foo' }
it { should be_false }
end
end
end
mark_as_paid.feature
Cucumber
1 2 3 4 5 6 7 8 9 10 11
Feature: Invoice should appear as paid
 
Background:
Given I am logged in as a user
And I have created an invoice with ID "123"
And I am on the invoice page for ID "123"
 
Scenario: Mark bill as paid
When I click on "Mark as paid"
Then I should see a notice that "Invoice #123 has been marked as paid"
And the "Paid" badge should appear over the invoice

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.