Created
October 14, 2008 18:46
-
-
Save insignia/16761 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreatePersonas < ActiveRecord::Migration | |
def self.up | |
create_table :personas do |t| | |
t.string :nombre | |
t.string :apellido | |
t.string :email | |
t.boolean :activo | |
t.timestamps | |
end | |
end | |
def self.down | |
drop_table :personas | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Persona < ActiveRecord::Base | |
validates_presence_of :nombre, :apellido, :email | |
validates_uniqueness_of :email | |
def self.activos | |
find(:all, :conditions => {:activo => true}) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') | |
module PersonaRspecHelper | |
def valid_attributes | |
{ | |
:nombre => "lorem", | |
:apellido => "ipsum", | |
:email => "[email protected]", | |
:activo => true | |
} | |
end | |
end | |
describe Persona do | |
include PersonaRspecHelper | |
before(:each) do | |
@persona = Persona.new | |
end | |
it "debería ser válido" do | |
@persona.attributes = valid_attributes | |
@persona.should be_valid | |
end | |
it "no debería aceptar un registro sin nombre" do | |
@persona.should validate_presence_of(:nombre) | |
end | |
it "no debería aceptar un registro sin apellido" do | |
@persona.should validate_presence_of(:apellido) | |
end | |
it "no debería aceptar un registro sin email" do | |
@persona.should validate_presence_of(:email) | |
end | |
it "no debería aceptar un email duplicado" do | |
Persona.create(valid_attributes) | |
lambda{ | |
Persona.create(valid_attributes) | |
}.should_not change(Persona, :count) | |
end | |
end | |
describe Persona, "activos" do | |
include PersonaRspecHelper | |
before(:each) do | |
@persona_activa = Persona.create(valid_attributes) | |
@persona_no_activa = Persona.new(valid_attributes) | |
@persona_no_activa.activo = false | |
@persona_no_activa.save | |
end | |
it "debería incluir los registros activos" do | |
Persona.activos.should include(@persona_activa) | |
end | |
it "no debería incluir los registros inactivos" do | |
Persona.activos.should_not include(@persona_no_activa) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# objeto | |
class Persona | |
attr_accessor :nombre, :apellido | |
def initialize(options={}) | |
@nombre = options[:nombre] ? options[:nombre] : nil | |
@apellido = options[:apellido] ? options[:apellido] : nil | |
end | |
def nombre_y_apellido | |
"#{@nombre} #{@apellido}" | |
end | |
def valido? | |
[email protected]? && [email protected]? | |
end | |
end | |
# spec | |
describe Persona do | |
before do | |
@persona = Persona.new(:nombre => 'lorem', :apellido => 'ipsum') | |
end | |
it "debería responder al metodo nombre" do | |
@persona.should respond_to(:nombre) | |
end | |
it "debería responder al metodo apellido" do | |
@persona.should respond_to(:apellido) | |
end | |
it "debería ser válido si tiene apellido y nombre" do | |
@persona.should be_valido | |
end | |
end | |
describe Persona, "inicializacion" do | |
it "debería asignar el nombre y el apellido" do | |
@persona = Persona.new(:nombre => 'lorem', :apellido => 'ipsum') | |
@persona.nombre.should eql("lorem") | |
@persona.nombre.should_not be_nil | |
@persona.apellido.should eql("ipsum") | |
@persona.apellido.should_not be_nil | |
end | |
it "debería asignar nil si no paso ningun valor" do | |
@persona = Persona.new | |
@persona.nombre.should eql(nil) | |
@persona.apellido.should eql(nil) | |
end | |
end | |
describe Persona, "valido?" do | |
it "debería ser false si tiene nombre y apellido vacios" do | |
@persona = Persona.new | |
@persona.should_not be_valido | |
end | |
it "debería ser true si tiene nombre y apellido" do | |
@persona = Persona.new(:nombre => 'lorem', :apellido => 'ipsum') | |
@persona.should be_valido | |
end | |
end | |
describe Persona, "nombre_y_apellido" do | |
before do | |
@persona = Persona.new(:nombre => 'lorem', :apellido => 'ipsum') | |
end | |
it "debería concatenar nombre y apellido" do | |
@persona.nombre_y_apellido.should_not be_nil | |
@persona.nombre_y_apellido.should == "lorem ipsum" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment