Skip to content

Instantly share code, notes, and snippets.

@dnagir
Created January 24, 2012 02:54
Show Gist options
  • Save dnagir/1667512 to your computer and use it in GitHub Desktop.
Save dnagir/1667512 to your computer and use it in GitHub Desktop.
How the simples thing (its gem) makes specs much easier to read
# Controller spec using plain old matchers
describe "#create" do
let(:participation) { stub 'Participation' }
let(:params) { {:participation => {}} }
before { InviteUser.stub(:into_company_by_params => participation) }
context "with success" do
before { participation.stub(:valid? => true) }
it "should assign participation" do
get(:create, params)
assigns(:participation).should == participation
end
it "should should redirect" do
get(:create, params).should be_redirect
end
end
context "with failure" do
before { participation.stub(:valid? => false) }
before { participation.should_receive(:available_developments).and_return devs }
let(:devs) { [stub, stub] }
it "should assign participation" do
get(:create, params)
assigns(:participation).should == participation
end
it "should render template" do
get(:create, params).should render_template :new
end
end
end
# Controller spec using `its` gem
describe "#create" do
let(:participation) { stub 'Participation' }
let(:params) { {:participation => {}} }
before { InviteUser.stub(:into_company_by_params => participation) }
subject { get(:create, params); self }
context "with success" do
before { participation.stub(:valid? => true) }
its(:assigns, :participation) { should == participation }
its(:response) { should be_redirect }
end
context "with failure" do
before { participation.stub(:valid? => false) }
before { participation.should_receive(:available_developments).and_return devs }
let(:devs) { [stub, stub] }
its(:assigns, :participation) { should == participation }
its(:response) { should render_template :new }
end
end
@dnagir
Copy link
Author

dnagir commented Jan 24, 2012

For curious ones, the gem to allow this is its.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment