2013-05-05 100 views
-1

我现在在Hartl的tutoral Ch.8。 我刚刚完成创建登录/注销功能,它的工作正常,但测试总是失败。我找不到解决方案。Rails Rspec问题:未定义方法'has_link'

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)>' 

和一些附加信息:

的routes.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> 

我的Gemfile

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' 

希望你能帮助我。 谢谢。

+0

has_link等来自水豚 - 你有安装? – 2013-05-05 09:56:32

+0

是的,我有这个宝石'宝石'水豚','〜> 2.1.0'',我使捆绑安装 – 2013-05-05 09:59:52

+0

为什么减号?我真的没有发现任何类似的问题,谷歌并没有帮助我找到答案。 – 2013-05-05 10:43:06

回答

1

您的错误消息表示失败的方法查找路径./spec/requests/authentication_pages_spec.rb;一掠而过的自述时,它不是超级明显,但在水豚2.X,这改变了:

If you are using Rails, put your Capybara specs in spec/features .

If you are not using Rails, tag all the example groups in which you want to use Capybara with :type => :feature .

您正在关注的水豚1.X可能写的教程,这就是为什么它告诉你把这些规格放在spec/requests而不是spec/features

+0

我改变了水豚ver。到1.1.2,现在是其中一个测试工作,但出现另一个问题 - “失败/错误:它{应该有链接('档案',href:用户路径(用户))} 预期的链接”档案“返回的东西'和同样的事情也注销链接。 – 2013-05-05 16:20:16

+0

@ you-rick转储测试中的响应主体并查看该链接是否确实存在。 – 2013-05-05 16:34:53

+0

我该如何倾倒它? – 2013-05-05 17:50:20

0

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

subject { page } 

这样:

require 'spec_helper' 

describe "Authentication" do 

    subject { page } 
    .. 

我没有这个问题了。

相关问题