2014-03-25 31 views
0

我正在參加RoR教程。當所有的rspec測試都應該變成綠色時,我就不能讓我的工作。我運行包的exec rspec的投機/和四個測試,一個失敗,此錯誤:失敗/錯誤:它{should_not have_selector('div.alert.alert-error')}

Failures:

1) Authentication signin with invalid information after visiting another page

Failure/Error: it { should_not have_selector('div.alert.alert-error') } 
    expected #has_selector?("div.alert.alert-error") to return false, got true 
# ./spec/requests/authentication_pages_spec.rb:25:in `block (5 levels) in <top (required)>'Finished in 7.2 seconds 62 examples, 1 

failure

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:25 # Authentication signin wi th invalid information after visiting another page

我authentication_pages_spec.rb文件:

require 'spec_helper' 

describe "Authentication" do 

    subject { page } 

    describe "signin page" do 
    before { visit signin_path } 

    it { should have_content('Sign in') } 
    it { should have_title('Sign in') } 
    end 

describe "signin" do 
    before { visit signin_path } 

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

     it { should have_title('Sign in') } 
     it { should have_selector('div.alert.alert-error') } 

    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_title(user.name) } 
     it { should have_link('Profile',  href: user_path(user)) } 
     it { should have_link('Settings', href: edit_user_path(user)) } 
     it { should have_link('Sign out', href: signout_path) } 
     it { should_not have_link('Sign in', href: signin_path) } 



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

describe "authorization" do 

    describe "for non-signed-in users" do 
     let(:user) { FactoryGirl.create(:user) } 

     describe "in the Users controller" do 

     describe "visiting the edit page" do 
      before { visit edit_user_path(user) } 
      it { should have_title('Sign in') } 
     end 

     describe "submitting to the update action" do 
      before { patch user_path(user) } 
      specify { expect(response).to redirect_to(signin_path) } 
     end 
     end 
    end 
    end 
end 

application.html.erb

<!DOCTYPE html> 
<html> 
    <head> 
    <title><%= full_title(yield(:title)) %></title> 
    <%= stylesheet_link_tag "application", media: "all", 
              "data-turbolinks-track" => true %> 
    <%= javascript_include_tag "application", "data-turbolinks-track" => true %> 
    <%= csrf_meta_tags %> 
    <%= render 'layouts/shim' %> 
    </head> 
    <body> 
    <%= render 'layouts/header' %> 
    <div class="container"> 
     <% flash.each do |key, value| %> 
     <%= content_tag(:div, value, class: "alert alert-#{key}") %> 
     <% end %> 
     <%= yield %> 
     <%= render 'layouts/footer' %> 
     <%= debug(params) if Rails.env.development? %> 
    </div> 
    </body> 
</html> 

home.html.erb:

<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/' %> 

static_pages_controller.rb:

class StaticPagesController < ApplicationController 
    def home 
    end 

    def help 
    end 

    def about 
    end 

    def contact 
    end 
end 
+0

你能分享問題中的主頁嗎? –

+0

@detskai你可以顯示你的佈局代碼,即'app/views/layouts/application.html.erb'嗎? –

+0

@Sachin Singh ok – detskai

回答

1

請確保您有sessions_controller.rb這樣定義的create動作:

def create  
    user = User.find_by(email: params[:email].downcase) 
    if user && user.authenticate(params[:password]) 
     # If the user exists and they give a correct password, 
     # sign the user in and redirect to the user's show page. 
     sign_in user 
     redirect_to user 
    else 
     # Create an error message and re-render the signin form. 
     flash.now[:error] = 'Invalid email/password combination' 
     render 'new' 
    end 
end 

,請注意flash.now。這與我們在本教程其他章節中使用的flash不一樣。