ho appena scritto un test per la prova, se una nuova creazione utente compone anche di un ambiente di amministrazione. Ecco la prova:convalida Rspec fallito - attributo non può essere vuoto, ma non è vuoto
describe User do
before(:each) do
@attr = {
:name => "Example User",
:email => "[email protected]",
:admin => "f"
}
end
it "should create a new instance given valid attributes" do
User.create!(@attr)
end
it "should require a name" do
no_name_user = User.new(@attr.merge(:name => ""))
no_name_user.should_not be_valid
end
it "should require an email" do
no_email_user = User.new(@attr.merge(:email => ""))
no_email_user.should_not be_valid
end
it "should require an admin setting" do
no_admin_user = User.new(@attr.merge(:admin => ""))
no_admin_user.should_not be_valid
end
end
Poi, nel mio modello User ho:
class User < ActiveRecord::Base
attr_accessible :name, :email, :admin
has_many :ownerships
has_many :projects, :through => :ownerships
email_regex = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :name, :presence => true,
:length => { :maximum => 50 }
validates :email, :presence => true,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => false }
validates :admin, :presence => true
end
ho chiaramente creato un nuovo utente con un ambiente di amministrazione, quindi perché è dicendo che è falso? Ho creato la migrazione per l'impostazione dell'amministratore come admin: boolean. Ho fatto qualcosa di male?
Ecco l'errore:
Failures:
1) User should create a new instance given valid attributes
Failure/Error: User.create!(@attr)
ActiveRecord::RecordInvalid:
Validation failed: Admin can't be blank
# ./spec/models/user_spec.rb:14:in `block (2 levels) in <top (required)>'
Stranamente, quando io commento Convalida: admin,: presenza => true, il test crea l'utente in modo corretto, ma non riesce a "utente deve richiedere un'impostazione admin"
EDIT: Quando cambio il @attr: valore di amministrazione per "t" funziona! Perché non funziona quando il valore è falso?
L'errore effettivo potrebbe essere d'aiuto. – bricker
Fallimenti: 1) l'utente deve creare una nuova istanza data valida attributi Fallimento/Errore: User.create (@ attr) ActiveRecord :: RecordInvalid: Convalida non riuscita: Admin non può essere vuoto # ./spec /models/user_spec.rb:14:in 'block (2 livelli) in ' –