# Teste subject.size its(:size) { should eq 4 } # Teste subject.empty? it { should_not be_empty } # Teste subject.size, subject.clear et subject.size encore it do expect { subject.clear }.to change(subject, :size).to(0) end end
do before { @user = User.new(:roles => [:admin]) } context "without scope" do subject { Product.build(:as => @user) } its(:owner) { should eq @user } it { should_not be_available } end context "with available scope" do subject { Product.available.build(:as => @user) } its(:owner) { should eq @user } it { should be_available } end end end end
subject { Product.new } it "returns the next sold out product" do subject.similar_products(10).first.should be_sold_out end end context "with supply" do subject { Product.new(:quantity_left => 5) } it "returns the next available product" do subject.similar_products(10).first.should_not be_sold_out end end end end
{ Product.new(:sold_out => false) } specify do subject.quantity_left = 0 expect { subject.save! }.to change(subject, :sold_out?).from(false).to(true) end end end end class Product < ActiveRecord::Base before_save :adjust_sold_out_flag, :if => proc { self.quantity_left_changed? and self.quantity_left == 0 } end
{ Product.new(:sold_out => false) } before { subject.quantity_left = 0 } specify do subject.should_receive(:adjust_sold_out_flag).once subject.save! end end end end class Product < ActiveRecord::Base before_save :adjust_sold_out_flag, :if => proc { self.quantity_left_changed? and self.quantity_left == 0 } end
# GET /products describe :index do end # PUT /products/:id describe :update do end # GET /products/:id describe :show do end # DELETE /products/:id describe :destroy do end end
do post :create, :product => { :name => "Foo" } end it { should redirect_to(products_path) } it { should set_the_flash[:notice].to("Yay!") } end end class ProductsController < ApplicationController def create if Product.create(params[:product]) flash[:notice] = "Yay!" redirect_to products_path end end end
:create_product! { post :create, :product => attrs } context "with valid attributes" do let(:attrs) { { :name => "Foo" } } before { create_product! } it { should redirect_to(products_path) } it { should set_the_flash[:notice].to("Yay!") } end context "with invalid attributes" do let(:attrs) { { :name => "" } } before { create_product! } it { should render_template("products/create") } it { should set_the_flash[:error].to("Nope!") } end end end
do @product = stub_model(Product) Product.stub(:all).returns([@product]) get :index end it { should respond_with(:success) } it { should render_template("products/index") } it { should assign_to(:products).with([@product]) } end end class ProductsController < ApplicationController def index @products = Product.all end end