2012-06-14 79 views
0

我一直運行到相同的測試失敗,我無法弄清楚爲什麼。我是編程新手,所以自從我上次通過測試以來,我已經發布了所有我曾經工作過的文件。測試rspec Hartl教程第10.3.3節

任何有識之士將不勝感激!

Failures: 

    1) Static pages Home page for signed-in users should render the user's feed 
Failure/Error: page.should have_selector("li##{item.id}", text: item.content) 
    expected css "li#2" with text "Dolor sit amet" to return something 
# ./spec/requests/static_pages_spec.rb:31:in `block (5 levels) in <top (required)>' 
# ./spec/requests/static_pages_spec.rb:30:in `block (4 levels) in <top (required)>' 

Finished in 5.66 seconds 
101 examples, 1 failure 

Failed examples: 

rspec ./spec/requests/static_pages_spec.rb:29 # Static pages Home page for signed-in users should  render the user's feed 

這裏是static_pages_spec.rb:

require 'spec_helper' 

describe "Static pages" do 
    subject { page } 

    shared_examples_for "all static pages" do 
    it { should have_selector('h1', text: heading) } 
    it { should have_selector('title', text: full_title(page_title)) } 
end 

    describe "Home page" do 
    before { visit root_path } 
    let(:heading) { 'Sample App' } 
    let(:page_title) { '' } 

    it_should_behave_like "all static pages" 
    it { should_not have_selector 'title', text: '| Home' } 


    describe "for signed-in users" do 
     let(:user) { FactoryGirl.create(:user) } 
     before do 
     FactoryGirl.create(:micropost, user: user, content: "Lorem ipsum") 
     FactoryGirl.create(:micropost, user: user, content: "Dolor sit amet") 
     sign_in user 
     visit root_path 
     end 

    it "should render the user's feed" do 
    user.feed.each do |item| 
     page.should have_selector("li##{item.id}", text: item.content) 
    end 
    end 
end 
end 

    describe "Help page" do 
    before { visit help_path } 
    let(:heading) { 'Help' } 
    let(:page_title) { 'Help' } 
end 

    describe "About Us page" do 
    before { visit about_path } 
    let(:heading) { 'About Us' } 
    let(:page_title) { 'About Us' } 
end 

    describe "Contact page" do 
    before { visit contact_path } 
    let(:heading) { 'Contact' } 
    let(:page_title) { 'Contact' } 
end 

end 

這裏是user_spec.rb

require 'spec_helper' 

describe User do 

    before do 
    @user = User.new(name: "Example User", email: "[email protected]", 
        password: "foobar", password_confirmation: "foobar") 
    end 

    subject { @user } 

    it { should respond_to(:name) } 
    it { should respond_to(:email) } 
    it { should respond_to(:password_digest) } 
    it { should respond_to(:password) } 
    it { should respond_to(:password_confirmation) } 
    it { should respond_to(:remember_token) } 
    it { should respond_to(:authenticate) } 

    it { should be_valid } 

    it { should respond_to(:admin) } 
    it { should respond_to(:authenticate) } 

    it { should respond_to(:authenticate) } 
    it { should respond_to(:microposts) } 
    it { should respond_to(:feed) } 

    it { should be_valid } 
    it { should_not be_admin } 

    describe "with admin attribute set to 'true'" do 
    before { @user.toggle!(:admin) } 

    it { should be_admin } 
    end 


    describe "when password is not present" do 
    before { @user.password = @user.password_confirmation = " " } 
    it { should_not be_valid } 
    end 

    describe "when password doesn't match confirmation" do 
    before { @user.password_confirmation = "mismatch" } 
    it { should_not be_valid} 
    end 

    describe "when password confirmation is nil" do 
    before { @user.password_confirmation = nil } 
    it { should_not be_valid } 
    end 

    describe "with a password that's too short" do 
    before { @user.password = @user.password_confirmation = "a" * 5} 
    it { should be_invalid } 
    end 

    describe "return value of authenticate method" do 
    before { @user.save } 
    let(:found_user) { User.find_by_email(@user.email) } 

    describe "with valid password" do 
     it { should == found_user.authenticate(@user.password) } 
    end 

    describe "with invalid password" do 
     let(:user_for_invalid_password) { found_user.authenticate("invalid") } 

     it { should_not == user_for_invalid_password } 
     specify { user_for_invalid_password.should be_false } 
    end 
    end 


    describe "remember token" do 
    before { @user.save } 
    its(:remember_token) { should_not be_blank } 
    end 

    describe "when name is not present" do 
    before { @user.name = " " } 
    it { should_not be_valid } 
    end 

    describe "when name is too long" do 
    before { @user.name = "a" * 51 } 
    it { should_not be_valid } 
    end 

    describe "when email is not present" do 
    before { @user.email = " " } 
    it { should_not be_valid} 
    end 

    describe "when email format is invalid" do 
    it "should be invalid" do 
     addresses = %w[[email protected],com user_at_foo.org [email protected] 
         [email protected]_baz.com [email protected]+baz.com] 
     addresses.each do |invalid_address| 
     @user.email = invalid_address 
     @user.should_not be_valid 
     end  
    end 
    end 

    describe "when email format is valid" do 
    it "should be valid" do 
     addresses = %w[[email protected] [email protected] [email protected] [email protected]] 
     addresses.each do |valid_address| 
     @user.email = valid_address 
     @user.should be_valid 
     end  
    end 
    end 
    describe "when email address is already taken" do 
    before do 
     user_with_same_email = @user.dup 
     user_with_same_email.email = @user.email.upcase 
     user_with_same_email.save 
    end 

    it { should_not be_valid } 
    end 

    describe "micropost associations" do 

    before { @user.save } 
    let!(:older_micropost) do 
     FactoryGirl.create(:micropost, user: @user, created_at: 1.day.ago) 
    end 
    let!(:newer_micropost) do 
     FactoryGirl.create(:micropost, user: @user, created_at: 1.hour.ago) 
    end 

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

    describe "status" do 
     let(:unfollowed_post) do 
     FactoryGirl.create(:micropost, user: FactoryGirl.create(:user)) 
     end 

     its(:feed) { should include(newer_micropost) } 
     its(:feed) { should include(older_micropost) } 
     its(:feed) { should_not include(unfollowed_post) } 
    end 



    it "should destroy associated microposts" do 
     microposts = @user.microposts 
     @user.destroy 
     microposts.each do |micropost| 
     Micropost.find_by_id(micropost.id).should be_nil 
     end 
    end 
    end 
end 

這裏是user.rb

class User < ActiveRecord::Base 

    attr_accessible :email, :name, :password, :password_confirmation 
    has_secure_password 
    has_many :microposts, dependent: :destroy 

    before_save { |user| user.email = email.downcase } 
    before_save :create_remember_token 

    validates :name, presence: true, length: { maximum: 50 } 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, 
        uniqueness: { case_sensitive: false } 
    validates :password, presence: true, length: { minimum: 6 } 
    validates :password_confirmation, presence: true 

    def feed 
    Micropost.where("user_id = ?", id) 
    end 

    private 

    def create_remember_token 
     self.remember_token = SecureRandom.urlsafe_base64 
    end 
end 

這裏是static_pages_controller.rb

class StaticPagesController < ApplicationController 

    def home 
    if signed_in? 
     @micropost = current_user.microposts.build 
     @feed_items = current_user.feed.paginate(page: params[:page]) 
    end 
    end 

    def help 
    end 

    def about 
    end 

    def contact 
    end 

end 

這裏是feed.html.erb

<% if @feed_items.any? %> 
<ol class="microposts"> 
    <% render partial: 'shared/feed_item', collection: @feed_items %> 
</ol> 
<%= will_paginate @feed_items %> 

這裏是feed_item.html.erb

<li id="<%= feed_item.id %>"> 
    <%= link_to gravatar_for(feed_item.user), feed_item.user %> 
    <span class="user"> 
    <%= link_to feed_item.user.name, feed_item.user %> 
    </span> 
    <span class="content"><%= feed_item.content %></span> 
    <span class="timestamp"> 
    Posted <%= time_ago_in_words(feed_item.created_at) %> ago. 
    </span> 
</li> 

這裏是home.html.erb

<% if signed_in? %> 
<div class="row"> 
    <aside class="span4"> 
     <section> 
      <%= render 'shared/user_info' %> 
     </section> 
     <section> 
      <%= render 'shared/micropost_form' %> 
     </section> 
    </aside> 
    <div class="span8"> 
     <h3>Micropost Feed</h3> 
     <%= render 'shared/feed' %> 
    </div> 
</div> 
<% else %> 
<div class="center hero-unit"> 

<h1>Welcome to the Sample App</h1> 

<h2> 
    This is the home page for the 
    <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a> 
    sample application. 
</h2> 

<%= link_to "Sign up now!", signup_path, class: "btn btn-large btn-primary" %> 
</div> 

<%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %> 
<% end %> 

這裏是微型posts_controller.rb

class MicropostsController < ApplicationController 
    before_filter :signed_in_user 

    def create 
    @micropost = current_user.microposts.build(params[:micropost]) 
    if @micropost.save 
     flash[:success] = "Micropost created!" 
     redirect_to root_path 
    else 
     @feed_items = [ ] 
     render 'static_pages/home' 
    end 
    end 

    def destroy 
    end 
end 

回答

1

我想通了。 feed.html.erb在渲染前缺少=。所以之前,它是這樣的:

<% if @feed_items.any? %> 
<ol class="microposts"> 
    <% render partial: 'shared/feed_item', collection: @feed_items %> 
</ol> 
<%= will_paginate @feed_items %> 

但它需要像這樣(的變化是在3號線):

<% if @feed_items.any? %> 
<ol class="microposts"> 
    <%= render partial: 'shared/feed_item', collection: @feed_items %> 
</ol> 
<%= will_paginate @feed_items %> 

所以,我想這是因爲隨着=使得<%紅寶石評估表達,然後將其付諸行動。

0

你的測試結果表明,你的home文件必須使第二項立文字「悲坐AMET」

我猜你一定與內容製作飼料項目「悲坐阿梅德」

相關問題