Create a gist now

Instantly share code, notes, and snippets.

@wuputah / Secret
Created Sep 22, 2010

What would you like to do?


  • 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?.


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).

class Invoice < ActiveRecord::Base
def paid?
state == 'paid'
describe Invoice
# normally would use factory_girl here, actually.
let(:invoice) { => state) }
let(:state) { 'unpaid' }
describe '#paid?' do
subject { invoice.paid? }
context 'when state is paid' do
let(:state) { 'paid' }
it { should be_true }
context 'when state equals other values' do
let(:state) { 'foo' }
it { should be_false }
Feature: Invoice should appear as paid
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment