-1

我现在在 Hartl 的教程 Ch.8 上。我刚刚完成创建登录/注销功能,它工作正常,但测试总是失败。我找不到解决方案。

Failures:

  1) Authentication signin with valid information
     Failure/Error: it { should have_link('Profile',  href: user_path(user)) }
     NoMethodError:
       undefined method `has_link?' for #<ActionDispatch::TestResponse:0x5175738
>
     # ./spec/requests/authentication_pages_spec.rb:39:in `block (4 levels) in <
top (required)>'

  2) Authentication signin with valid information
     Failure/Error: it { should have_link('Sign out',   href: signout_path) }
     NoMethodError:
       undefined method `has_link?' for #<ActionDispatch::TestResponse:0x5395338
>
     # ./spec/requests/authentication_pages_spec.rb:40:in `block (4 levels) in <
top (required)>'

  3) Authentication signin with valid information
     Failure/Error: it { should_not have_link('Sign in', href: signin_path) }
     NoMethodError:
       undefined method `has_link?' for #<ActionDispatch::TestResponse:0x56d6870
>
     # ./spec/requests/authentication_pages_spec.rb:41:in `block (4 levels) in <
top (required)>'

  4) Authentication signin with valid information followed by signout
     Failure/Error: before { click_link "Sign out" }
     ActionController::RoutingError:
       No route matches [GET] "/signout"
     # ./spec/requests/authentication_pages_spec.rb:44:in `block (5 levels) in <
top (required)>'

  5) UserPages signup page with valid information after saving the user
     Failure/Error: it { should have_link('Sign out',   href: signout_path) }
     NoMethodError:
       undefined method `has_link?' for #<ActionDispatch::TestResponse:0x53fb830
>
     # ./spec/requests/user_pages_spec.rb:50:in `block (5 levels) in <top (requi
red)>'

还有一些附加信息:

路线.rb

resources :users   
resources :sessions, only: [:new, :create, :destroy]

match '/contact', :to => 'pages#contact'

match '/about',   :to => 'pages#about'  

match '/help',    :to => 'pages#help'

match '/signup',  :to => 'users#new'  

match '/signin',  :to => 'sessions#new' 

match '/signout', :to => 'sessions#destroy', :via => :delete

root :to => 'pages#home'

测试代码

require 'spec_helper'

describe "Authentication" do

  subject { response }

  describe "signin page" do
    before { visit signin_path }

    it { should have_selector('h1',    content: 'Sign in') }
    it { should have_selector('title', content: 'Sign in') }
  end

  describe "signin" do
    before { visit signin_path }

    describe "with invalid information" do
      before { click_button "Sign in" }

      it { should have_selector('title', content: 'Sign in') }
      it { should have_selector('div.alert.alert-error', content: 'Invalid') }

      describe "after visiting another page" do
        before { click_link "Home" }
        it { should_not have_selector('div.alert.alert-error') }
      end
    end


  describe "with valid information" do
      let(:user) { FactoryGirl.create(:user) }
      before do
        fill_in "Email",    with: user.email.upcase
        fill_in "Password", with: user.password
        click_button "Sign in"
      end

      it { should have_selector('title',    content: user.name) }
      it { should have_link('Profile',      href: user_path(user)) }
      it { should have_link('Sign out',     href: signout_path) }
      it { should_not have_link('Sign in',  href: signin_path) }

      describe "followed by signout" do
        before { click_link "Sign out" }
        it { should have_link('Sign in') }
      end

    end


  end
end

标题 html

<ul class="nav no-margin">
    <li><%= link_to "Home", root_path %></li>
    <li><%= link_to "Help", help_path %></li>
    <% if signed_in? %>
    <li><%= link_to "Users", '#' %></li>
    <li id="fat-menu" class="dropdown">
      <a href="#" class="dropdown-toggle" data-toggle="dropdown">
        Account <b class="caret"></b>
      </a>
     <ul class="dropdown-menu">
    <li><%= link_to "Profile", current_user %></li>
    <li><%= link_to "Settings", '#' %></li>
    <li class="divider"></li>
    <li>
      <%= link_to "Sign out", signout_path, method: "delete" %>
    </li>
     </ul>
    </li>
     <% else %>
    <li><%= link_to "Sign in", signin_path %></li>
     <% end %>
      <li><%= link_to "Sign up now!", signup_path, :class => "signup_button" %></li>
</ul>

我的宝石文件

source 'https://rubygems.org'

gem 'rails', '3.2.1'

gem 'sqlite3', :group => [:development, :test]
group :production do
  gem 'thin'
  gem 'pg'
end

group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier', '>= 1.0.3'
  gem 'bootstrap-sass-rails'
  gem 'bcrypt-ruby', '3.0.1'

  gem 'uglifier', '>= 1.0.3'
end

group :development do
  gem 'rspec-rails', '2.6.1'
  gem 'annotate', '~> 2.4.1.beta' 
end

group :test do
  gem 'rspec-rails', '2.6.1'
  gem 'webrat', '0.7.1'
  gem 'spork', '0.9.0.rc8'
  gem 'ZenTest', '4.8.3'
  gem 'capybara', '~> 2.1.0'
  gem 'factory_girl_rails', '4.1.0'
end


gem 'jquery-rails'

希望您能够帮助我。谢谢。

4

2 回答 2

1

您的错误消息说失败的方法查找在 path ./spec/requests/authentication_pages_spec.rb;浏览自述文件​​时,这并不是很明显,但在 Capybara 2.X 中,这发生了变化:

如果您使用的是 Rails,请将您的 Capybara 规范放入spec/features.

如果您不使用 Rails,请将要在其中使用 Capybara 的所有示例组标记为:type => :feature.

您正在遵循的教程可能是为 Capybara 1.X 编写的,这就是为什么它告诉您将这些规范放入spec/requests而不是spec/features.

于 2013-05-05T15:55:40.467 回答
0

我有同样的问题,但我不确定它是否相同。尝试添加:

subject { page }

这边走:

require 'spec_helper'

describe "Authentication" do

  subject { page }
  ..

我再也没有这个问题了。

于 2014-05-21T14:56:25.453 回答