测试失败Michael Hartl Ruby on Rails Turorial Chapter 10.28

时间:2012-10-22 21:26:18

标签: ruby-on-rails railstutorial.org

我使用Michael Hartl的教程。目前我在chapter 10。在第10.28部分,当我尝试运行测试时:

bundle exec rspec spec/requests/authentication_pages_spec.rb

我收到此错误:

Failures:

  2) Authentication authorization for non-signed-in users in the Microposts
     controller submitting to the destroy action Failure/Error: before { delete   
     microposts_path (FactoryGirl.create(:micropost)) } ActionController::RoutingError:
     No route matches [DELETE] "/microposts.1" 
     # ./spec/requests/authentication_pages_spec.rb:115:in`block(6 levels) in
     <top required)>'

这是我的authentication_pages_spec.rb:

require 'spec_helper'

describe "Authentication" do
  let(:user) { FactoryGirl.create(:user) } 
    subject { page }

  describe "signin page" do
    before { visit signin_path }

    it { should have_selector('h1', text: 'Sign in') }
    it { should have_selector('title', text: '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', text: 'Sign in') }
      it { should have_selector('div.alert.alert-error', text: 'Invalid') }

      describe "after visiting another page" do
          before { click_link "Home"}
          it { should 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
        fill_in "Password", with: user.password
        click_button "Sign in"
      end

      it { should have_selector('title',    text: user.name) }

      it { should have_link('Users',        href: users_path) }
      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 "followed 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 "when attempting to visit a protected page" do
        before do
          visit edit_user_path(user)
          fill_in "Email",    with: user.email
          fill_in "Password", with: user.password
          click_button "Sign in"
        end    

        describe "after signing in" do

          it "should render the desired protected page" do
            page.should have_selector('title', text: 'Edit user')
          end

          describe "when signing in again" do
            before do
              delete signout_path
              visit signin_path
              fill_in "Email",    with: user.email
              fill_in "Password", with: user.password
              click_button "Sign in"
            end

            it "should render the default (profile) page" do
              page.should have_selector('title', text: user.name)
            end
          end
        end
      end

      describe "in the Users controller" do

        describe "visiting the edit page" do
          before { visit edit user_path(user) }
          it { should have_selector('title', text: 'Sign in') }
        end

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

        describe "visiting user index" do
          before { visit users_path }
          it { should have_selector('title', text: 'Sign in') }
        end
      end  

      describe "in the Microposts controller" do

        describe "submitting to the create action" do
          before { post microposts_path }
          specify { response.should redirect_to(signin_path) }
        end

        describe "submitting to the destroy action" do
          before { delete microposts_path(FactoryGirl.create(:micropost)) }
          specify { response.should redirect_to(signin_path) }
        end
      end
    end        

    describe "as wrong user" do
      let(:user) { FactoryGirl.create(:user) }
      let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
      before { sign_in user }

      describe "visiting Users#edit page" do
        before { visit edit_user_path(wrong_user) }
        it { should have_selector('title', text: full_title('Edit user')) }
      end

      describe "submitting a PUT request to the Users#update action" do
        before { put user_path(wrong_user) }
        specify { response.should redirect_to(root_path)}
      end
    end

    describe "as non-admin user" do
      let(:user) { FactoryGirl.create(:user) }
      let(:non_admin) { FactoryGirl.create(:user) }

      before { sign_in non_admin }

      describe "submitting a DELETE request to the Users#destroy action" do
        before { delete user_path(user) }
        specify { response.should redirect_to(root_path) }
      end
    end
  end
end

Rake路线的结果:

            users GET    /users(.:format)             users#index
                  POST   /users(.:format)             users#create
         new_user GET    /users/new(.:format)         users#new
        edit_user GET    /users/:id/edit(.:format)    users#edit
             user GET    /users/:id(.:format)         users#show
                  PUT    /users/:id(.:format)         users#update
                  DELETE /users/:id(.:format)         users#destroy
         sessions POST   /sessions(.:format)          sessions#create
      new_session GET    /sessions/new(.:format)      sessions#new
          session DELETE /sessions/:id(.:format)      sessions#destroy
       microposts POST   /microposts(.:format)        microposts#create
        micropost DELETE /microposts/:id(.:format)    microposts#destroy
             root        /                            static_pages#home
           signup        /signup(.:format)            user#new
           signin        /signin(.:format)            sessions#new
          signout DELETE /signout(.:format)           sessions#destroy
static_pages_home        /static_pages/home(.:format) static_pages#home
             help        /help(.:format)              static_pages#help
            about        /about(.:format)             static_pages#about
          contact        /contact(.:format)           static_pages#contact

我尝试使用Micheal Hart'github更正我的代码,但没有结果。 有人知道吗?

1 个答案:

答案 0 :(得分:8)

在你的测试中,你写下这个:

before { delete microposts_path(FactoryGirl.create(:micropost)) }

这将告诉测试框架对您路线中DELETE创建的路径生成microposts_path请求。

您的rake routes输出具有以下内容:

   microposts POST   /microposts(.:format)        microposts#create
    micropost DELETE /microposts/:id(.:format)    microposts#destroy

查看上面的microposts vs micropost。您应该在测试中使用micropost_path