Skip to content

Instantly share code, notes, and snippets.

@joshchernoff joshchernoff/cart_spec.rb
Last active Aug 29, 2015

Embed
What would you like to do?
rspec mocking with has_many
require 'spec_helper'
describe Cart do
describe "#to_s" do
let(:cart) { FactoryGirl.create(:cart) }
context "when cart is empty" do
it{ subject.to_s.should == "Your cart is empty" }
end
context "when cart is not empty" do
before do
cart.stub(:items).and_return(stub_items(count))
end
context "when cart has 2 items" do
let(:count) { 2 }
it{ subject.to_s.should == "Your cart has 2 items" }
end
context "when cart has 3 items" do
let(:count) { 3 }
it{ subject.to_s.should == "Your cart has 3 items" }
end
end
end
end
def stub_items(count)
Array.new(count){
double(Item)
}
end
F
Failures:
1) Cart#to_s when cart is not empty when cart has 3 items should == "Your cart has 3 items"
Failure/Error: it{ subject.to_s.should == "Your cart has 3 items" }
expected: "Your cart has 3 items"
got: "Your cart is empty" (using ==)
# ./spec/models/cart_spec.rb:25:in `block (5 levels) in <top (required)>'
Finished in 0.04883 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/models/cart_spec.rb:25 # Cart#to_s when cart is not empty when cart has 3 items should == "Your cart has 3 items"
Randomized with seed 19050
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.