class SensibleController < ApplicationController def index @objects = Record.all end def new @object = Record.new end def create Record.create params[:record] redirect_to index_path end def edit @record = Record.find(params[:id]) end def update Record.find(params[:id]).update_attributes params[:record] redirect_to index_path end end Sunday, 1 July 12
INTEGRATION require 'spec_helper' describe ExampleWithModelController do describe 'post :create' do subject { post :create } it "creates a model" do expect { subject }.to change(RandomModel,:count).by(1) end it "redirects away" do subject.should redirect_to '/' end end end Sunday, 1 July 12
INTEGRATION require 'spec_helper' describe ExampleWithModelController do describe 'post :create' do before { RandomModel.stub(:create) } subject { post :create } it "creates a RandomModel" do RandomModel.should_receive(:create) subject end it "redirects away" do subject.should redirect_to '/' end end end Sunday, 1 July 12
class RandomModel; end describe ExampleWithModelController do describe '#create' do let(:controller) { described_class.new } before do controller.request = Struct.new(:parameters).new controller.stub(:redirect_to) end subject { controller.create } it "creates a RandomModel" do RandomModel.should_receive(:create) subject end it "redirects to away" do controller.should_receive(:redirect_to).with('/') subject end end end Sunday, 1 July 12
class ChangeStock def initialize(item) @item = item end def delta quantity - @item.quantity end def to(quantity) if quantity > @item.quantity @item.fee_generator.charge_increase delta end @item.set_quantity quantity @item.save! end end Sunday, 1 July 12
SUMMARY Require what you need Unit test your units Abstract away Integration/acceptance test framework Don’t boot rails unless needed Sunday, 1 July 12
SUMMARY Require what you need Unit test your units Abstract away Integration/acceptance test framework Don’t boot rails unless needed Be sensible! Sunday, 1 July 12