ruby-on-rails - 测试 rspec Hartl 教程第 10.3.3 章

标签 ruby-on-rails testing rspec railstutorial.org

我一直遇到同样的测试失败,我不明白为什么。我是编程新手,所以我发布了自上次通过测试以来我处理过的所有文件。

任何见解将不胜感激!

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: "user@example.com", 
                  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[user@foo,com user_at_foo.org example.user@foo.
                       foo@bar_baz.com foo@bar+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[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
      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+\-.]+@[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 %>

这是microposts_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

最佳答案

我想通了。 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 %> 

所以,我认为这是因为 <% with = 使 ruby​​ 计算表达式然后调用它来执行操作。

关于ruby-on-rails - 测试 rspec Hartl 教程第 10.3.3 章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11037547/

相关文章:

ruby - Rspec/mocks 引发未初始化的常量 BasicObject::RSpec

ruby-on-rails - rails 上的 ruby 。堆栈级别太深错误

ruby-on-rails - 事件管理员 文本编辑器 图片上传

ruby-on-rails - rails 轮胎 elasticsearch 奇怪的错误

python - Selenium Firefox 驱动程序从主机文件读取

rspec - 使用工厂女孩创建 has_many 关系的特征

ruby-on-rails - 每个循环使用 Rspec 的单元测试用例

ruby-on-rails - Rails 4.2.2-Windows 7/: bundle exec guard 的 git Bash 上未显示测试结果

email - 每天在生产服务器上测试 Rails 应用程序,并自动回复电子邮件

node.js - 在 NodeJS 中使用流进行 TDD/测试