Rspec:Capybara助手没找到链接

时间:2015-04-05 22:54:06

标签: ruby-on-rails rspec capybara factory-bot

构建一个基本的博客应用程序,让我学习TDD,所以我很新。我在让Capybara识别链接方面遇到了问题。似乎所有元素都已到位所以我对我所缺少的东西感到困惑。

这是失败:

  1) PostsController#GET create with valid attributes creates a post by the current_user
     Failure/Error: sign_in(build_stubbed(:user))
     Capybara::ElementNotFound:
       Unable to find link "Sign Up"
     # ./spec/support/authentication_helpers.rb:7:in `sign_in'
     # ./spec/controllers/posts_controller_spec.rb:44:in `block (4 levels) in <top (required)>'

规格/支持/ authentication_helper.rb

require 'rails_helper'

module AuthenticationHelpers

    def sign_in(x)
        visit root_path
        click_link "Sign Up"
        fill_in "First Name", with: x.first_name
        fill_in "Last Name", with: x.last_name
        fill_in "Email", with: x.email
        fill_in "Password", with: x.password
        fill_in "Password Confirmation", with: x.password
        click_button "Sign Up"
    end
end

规格/控制器/ posts_controller_spec.rb

describe '#GET create' do
    context 'with valid attributes' do
      before :each do
        post :create, post: attributes_for(:post)
      end

          it { expect(Post.count).to eq(1) }
          it { expect(flash[:success]).to eq('Your post has been saved!') }
          it { expect(assigns(:post)).to be_a(Post) }
          it { expect(assigns(:post)).to be_persisted }
          it { expect(response).to redirect_to Post.first }

          it "creates a post by the current_user" do
            sign_in(build_stubbed(:user))
            @post = Post.last
            expect(@post.user).to eq(user)
          end
    end

_header.html.erb

        <div class="cbp-af-header">
            <div class="cbp-af-inner">
                <nav>
                    <a href="#">About</a>
                    <a href="#">Services</a>
                    <a href="#"><%= link_to "Sign Up", new_user_path %></a>
                    <a href="#"><%= link_to "Log In", new_user_session_path %></a>
                </nav>
            </div>
        </div>

佣金路线

           Prefix Verb   URI Pattern                                 Controller#Action
            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
                  PATCH  /users/:id(.:format)                        users#update
                  PUT    /users/:id(.:format)                        users#update
                  DELETE /users/:id(.:format)                        users#destroy
    user_sessions POST   /user_sessions(.:format)                    user_sessions#create
 new_user_session GET    /user_sessions/new(.:format)                user_sessions#new
             root GET    /                                           posts#index
    post_comments GET    /posts/:post_id/comments(.:format)          comments#index
                  POST   /posts/:post_id/comments(.:format)          comments#create
 new_post_comment GET    /posts/:post_id/comments/new(.:format)      comments#new
edit_post_comment GET    /posts/:post_id/comments/:id/edit(.:format) comments#edit
     post_comment GET    /posts/:post_id/comments/:id(.:format)      comments#show
                  PATCH  /posts/:post_id/comments/:id(.:format)      comments#update
                  PUT    /posts/:post_id/comments/:id(.:format)      comments#update
                  DELETE /posts/:post_id/comments/:id(.:format)      comments#destroy
            posts GET    /posts(.:format)                            posts#index
                  POST   /posts(.:format)                            posts#create
         new_post GET    /posts/new(.:format)                        posts#new
        edit_post GET    /posts/:id/edit(.:format)                   posts#edit
             post GET    /posts/:id(.:format)                        posts#show
                  PATCH  /posts/:id(.:format)                        posts#update
                  PUT    /posts/:id(.:format)                        posts#update
                  DELETE /posts/:id(.:format)                        posts#destroy
            admin GET    /admin(.:format)                            admin/dashboard#index

如果此信息相关;身份验证是从头开始构建的,我还没有实现授权。

1 个答案:

答案 0 :(得分:0)

&#39;注册&#39;是一个链接,而不是一个按钮,因此您需要使用click_link(或click,它适用于这两种类型。)