Jus*_*zer 4 testing rspec ruby-on-rails ruby-on-rails-3
这是测试:
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
Run Code Online (Sandbox Code Playgroud)
这是错误:
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
Run Code Online (Sandbox Code Playgroud)
我认为错误可能在我的数据填充程序代码中的某处:
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
Run Code Online (Sandbox Code Playgroud)
如果我应该再复制我的代码,请告诉我.
更新:这@attr是在user_spec.rb文件顶部定义的位置:
require 'spec_helper'
describe User do
before(:each) do
@attr = {
:name => "Example User",
:email => "user@example.com",
:password => "foobar",
:password_confirmation => "foobar"
}
end
Run Code Online (Sandbox Code Playgroud)
检查以确保您没有进一步阻止使用相同电子邮件地址的块进行user_spec.rb呼叫.如果您的块嵌套不正确,您将收到此错误.例如,在Rails的教程,很容易不小心你的窝的内框,它使用相同的代码.User.createbefore(:each)describe "admin attribute"describe "password encryption"before(:each)
| 归档时间: |
|
| 查看次数: |
2635 次 |
| 最近记录: |