这是测试:
describe "admin attribute" do before(:each) do @user = User.create!(@attr) end it "should respond to admin" do @user.should respond_to(:admin) end it "should not be an admin by default" do @user.should_not be_admin end it "should be convertible to an admin" do @user.toggle!(:admin) @user.should be_admin end end
这是错误:
1) User password encryption admin attribute should respond to admin Failure/Error: @user = User.create!(@attr) ActiveRecord::RecordInvalid: Validation Failed: Email has already been taken # ./spec/models/user_spec.rb:128
require 'faker' namespace :db do desc "Fill database with sample data" task :populate => :environment do Rake::Task['db:reset'].invoke admin = User.create!(:name => "Example User",:email => "example@railstutorial.org",:password => "foobar",:password_confirmation => "foobar") admin.toggle!(:admin) 99.times do |n| name = Faker::Name.name email = "example-#{n+1}@railstutorial.org" password = "password" User.create!(:name => name,:email => email,:password => password,:password_confirmation => password) end end end
如果我应该再复制我的代码,请告诉我.
更新:这是@attr定义的位置,位于user_spec.rb文件的顶部:
require 'spec_helper' describe User do before(:each) do @attr = { :name => "Example User",:email => "user@example.com",:password_confirmation => "foobar" } end
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。