require "test/unit" class ProductTest < Test::Unit::TestCase def test_initial_quantity assert_equal 4, Product.new.initial_quantity end end describe Product do subject { Product.new } its(:initial_quantity) { should eq 4 } end
describe Array do subject { [1, 2, 3, 4] } # 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
% rspec array_spec.rb --format documentation --colour Array should not be empty should change size size should eq 4 Finished in 0.00316 seconds 3 examples, 0 failures
describe Product do context "without supply" do subject { Product.new(:quantity_left => 0) } it { should be_sold_out } end context "with supply" do subject { Product.new(:quantity_left => 10) } it { should_not be_sold_out } end end
describe Product do describe :build do context "as admin user" 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
describe Product do describe :similar_products do context "without supply" do 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
describe Product do describe :Associations do # TODO end describe :Validations do # TODO end describe :Callbacks do # TODO end describe :InstanceMethods do # TODO end describe :ClassMethods do # TODO end end
describe Product do describe :Associations do it { should belong_to(:brand) } it { should have_many(:pictures) } end end class Product < ActiveRecord::Base belongs_to :brand has_many :pictures end
describe Product do describe :Validations do it { should validate_presence_of(:name) } it { should ensure_inclusion_of(:state).in(%w{visible hidden}) } it { should_not allow_value("_$#!").for(:sku) } end end class Product < ActiveRecord::Base validates :name, :presence => true validates :state, :inclusion => { :in => %w{visible hidden} } validates :sku, :rosette_sku => true end
describe Product do describe :Callbacks do describe :adjust_sold_out_flag do subject { 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
describe Product do describe :Callbacks do describe :adjust_sold_out_flag do subject { 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
describe ProductsController do # POST /products describe :create do 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
describe ProductsController do # POST /products describe :create do before 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
class ProductsController < ApplicationController def create if Product.create(params[:product]) flash[:notice] = "Yay!" redirect_to products_path else flash[:error] = "Nope!" end end end
describe ProductsController do # POST /products describe :create do let :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
describe ProductsController do # GET /products describe :index do before 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
describe "products/index" do before do assign(:products, [stub_model(Product, :name => "Foo")]) render end it { expect(rendered).to include("Foo") } end
describe "GET /products" do it do expect(:get => "/products").to route_to( :controller => "products", :action => "index" ) end end resources :products Routes
describe ProductsHelper do describe :format_price do subject do helper.format_price(stub_model(Product, :price => 1200)) end it { should eq "$12.00" } end end module ProductsHelper def format_price(product) Money.new(product.price).format end end Helpers