第11.1章中的另一个Rails 3教程问题

时间:2011-06-12 00:27:59

标签: ruby-on-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 => "user@example.com"
      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                    "jd.skinner@me.com"
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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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