2011-06-12 83 views
0

我无法获得3个测试通过。这里是失败:第11.1章中的另一个Rails 3教程问题

Failures: 
1) Users micropost associations should have a micropost attribute 
Failure/Error: @mp1 = Factory(:micropost, :user => @user, :created_at => 1.day.ago) 
Validation failed: User can't be blank 
# ./spec/requests/users_spec.rb:69:in `block (3 levels) in <top (required)>' 

2) Users micropost associations should have the right microposts in the right order 
Failure/Error: @mp1 = Factory(:micropost, :user => @user, :created_at => 1.day.ago) 
Validation failed: User can't be blank 
# ./spec/requests/users_spec.rb:69:in `block (3 levels) in <top (required)>' 

3) Users micropost associations should destroy associated microposts 
Failure/Error: @mp1 = Factory(:micropost, :user => @user, :created_at => 1.day.ago) 
Validation failed: User can't be blank 
# ./spec/requests/users_spec.rb:69:in `block (3 levels) in <top (required)>' 

任何想法在哪里看,将不胜感激。我会发布你需要的任何代码示例来帮助我追踪事情。

非常感谢!

根据要求,这里是user_spec.rb

require 'spec_helper' 

describe "Users" do 
describe "signup" do  
describe "failure" do 

    it "should not make a new user" do 
    lambda do 
     visit signup_path 
     fill_in "Name",   :with => "" 
     fill_in "Email",  :with => "" 
     fill_in "Password",  :with => "" 
     fill_in "Confirmation", :with => "" 
     click_button 
     response.should render_template('users/new') 
     response.should have_selector("div#error_explanation") 
    end.should_not change(User, :count) 
    end 
end 

describe "success" do 
    it "should make a new user" do 
    lambda do 
     visit signup_path 
     fill_in "Name",   :with => "Example User" 
     fill_in "Email",   :with => "[email protected]" 
     fill_in "Password",  :with => "foobar" 
     fill_in "Confirmation", :with => "foobar" 
     click_button 
     response.should have_selector("div.flash.success", :content => "Welcome") 
     response.should render_template('users/show') 
    end.should change(User, :count).by(1) 
    end 
end 
end 

describe "sign in/out" do 

describe "failure" do 

    it "should not sign the user in" do 
    visit signin_path 
    fill_in :email, :with => "" 
    fill_in :password, :with => "" 
    click_button 
    response.should have_selector("div.flash.error", :content => "Invalid") 
    end 
end 

describe "success" do 

    it "should sign a user in and out" do 
    user = Factory(:user) 
    visit signin_path 
    fill_in :email, :with => user.email 
    fill_in :password, :with => user.password 
    click_button 
    controller.should be_signed_in 
    click_link "Sign out" 
    controller.should_not be_signed_in 
    end 
end 
end 

describe "micropost associations" do 

before(:each) do 
    @user = User.create(@attr) 
    @mp1 = Factory(:micropost, :user => @user, :created_at => 1.day.ago) 
    @mp2 = Factory(:micropost, :user => @user, :created_at => 1.hour.ago) 
end 

it "should have a micropost attribute" do 
    @user.should respond_to(:microposts) 
end 

it "should have the right microposts in the right order" do 
    @user.microposts.should == [@mp2, @mp1] 
end 

it "should destroy associated microposts" do 
    @user.destroy 
    [@mp1, @mp2].each do |micropost| 
    Micropost.find_by_id(micropost.id).should be_nil 
    end 
end 
end 
end 

而且factories.rb:

Factory.define :user do |user| 
user.name      "JD Skinner" 
user.email     "[email protected]" 
user.password     "password" 
user.password_confirmation "password" 
end 

Factory.sequence :email do |n| 
"person-#{n}@example.com" 
end 

Factory.define :micropost do |micropost| 
micropost.content "foo bar" 
micropost.association :user 
end 

太感谢你了!

-Kagi

+0

如何是“@ user”被创建还是获取?你是否创建了类似'factories.rb'文件的东西,并为该文件中的用户设置了详细信息? – 2011-06-12 00:29:57

+0

请发布您的users_spec.rb文件和您的factories.rb文件的内容。 – 2011-06-12 00:55:33

回答

0

您的用户需要是唯一的吗?如果是这样的......

Factory.define :user do |user| 
    user.sequence(:name) { |n| "Test-#{n}" } 
    user.sequence(:email) { |n| "test-user-#{n}@example.com" } 
    user.password "password" 
    user.password_confirmation "password" 
end 
+0

用户名并不是那么重要,但是电子邮件地址是工厂中间的顺序块。我是Rails的新手(显然),我猜你的方式只是另一种更简单的方法来做同样的事情? – YuKagi 2011-06-12 17:41:57

+0

您目前定义电子邮件的方式是尝试为每个模型使用相同的电子邮件。这可能是你的问题的一部分。您应该使用上面显示的电子邮件 - 也许您可以在其他地方定义该块(如您所做的那样),但您的用户定义必须显式调用序列方法,而不是采用文字电子邮件地址。 – Andrew 2011-06-12 23:29:08

0

我觉得Validation failed: User can't be blank,发生,因为你没有定义(或错误的价值观定义)@attr变量,在user_spec.rb

@user = User.create(@attr) 

试试这个

before(:each) do 
    @user = Factory(:user) 
    @mp1 = Factory(:micropost, :user => @user, :created_at => 1.day.ago) 
    @mp2 = Factory(:micropost, :user => @user, :created_at => 1.hour.ago) 
end 
相关问题