代码之家  ›  专栏  ›  技术社区  ›  user8359832

Capybara::ElementNotFound:找不到未禁用的可见字段“post[date]”

  •  0
  • user8359832  · 技术社区  · 7 年前

    我有一个集成测试,它给我带来的错误似乎与 fill_in

    require 'rails_helper'
    
    describe 'navigate' do
      let(:user) {FactoryGirl.create(:user)}
    
      let(:post) do
        Post.create(date: Date.today, rationale: "Rationale", user_id: user.id)
      end
    
      before do
        login_as(@user, :scope => :user)
      end
    
      describe 'index' do
        before do
          visit posts_path
        end
        it 'can be reached successfully' do
          expect(page.status_code).to eq(200)
        end
    
        it 'has a title of Posts' do
          expect(page).to have_content(/Posts/)
        end
    
        it 'has a list of posts' do
          post1 = FactoryGirl.build_stubbed(:post)
          post2 = FactoryGirl.build_stubbed(:second_post)
          visit posts_path
          expect(page).to have_content(/Rationale|content/)
        end
    
        it 'has a scope so that only post creators can see their posts' do
          other_user = User.create(first_name: 'Non', last_name: 'Authorized', email: 'nonauth@example.com',
                                              password: "password", password_confirmation: "password")
    
          post_from_other_user = Post.create(date: Date.today, rationale: "This post shouldn't be seen",
                                              user_id: other_user.id)
    
          visit posts_path
    
          expect(page).to_not have_content(/This post shouldn't be seen/)
        end
      end
    
      describe 'new' do
        it "has a link from the homepage" do
          visit root_path
    
          click_link("new_post_from_nav")
          expect(page.status_code).to eq(200)
        end
      end
    
      describe 'delete' do
        it 'can be deleted' do
          logout(:user)
    
          delete_user = FactoryGirl.create(:user)
          login_as(delete_user, :scope => :user)
    
          post_to_delete = Post.create(date: Date.today, rationale: 'rationale', user_id: delete_user.id)
    
          visit posts_path
    
          click_link("delete_post_#{post_to_delete.id}_from_index")
          expect(page.status_code).to eq(200)
        end
      end
    
      describe 'creation' do
        before do
          visit new_post_path
        end
    
        it 'has a new form that can be reached' do
          expect(page.status_code).to eq(200)
        end
    
        it 'can be created from a new form page' do
          fill_in 'post[date]', with: Date.today
          fill_in 'post[rationale]', with: "Some rationale"
          click_on "Save"
    
          expect(page).to have_content("Some rationale")
        end
    
        it 'will have a user associated with it' do
          fill_in 'post[date]', with: Date.today
          fill_in 'post[rationale]', with: "User Association"
          click_on "Save"
    
          expect(User.last.posts.last.rationale).to eq("User Association")
        end
      end
    
      describe 'edit' do
    
        it "can be edited" do
          visit edit_post_path(post)
    
          fill_in 'post[date]', with: Date.today
          fill_in 'post[rationale]', with: "Edited content"
          click_on "Save"
    
          expect(page).to have_content("Edited content")
        end
    
        it "cannot be edited by a non authorized user" do
          logout(:user)
          non_authorized_user = FactoryGirl.create(:non_authorized_user)
          login_as(non_authorized_user, :scope => :user)
    
          visit edit_post_path(post)
    
          expect(current_path).to eq(root_path)
        end
      end
    end
    

    我查看视图/post/\u表单。html。erb文件:

    <%= form_for @post, class: "form-horizontal" do |f| %>
      <% if @post.errors.any? %>
        <% @post.errors.full_messages.each do |error| %>
          <%= js add_gritter(error, title: "Overtime App Notification", sticky: false, image: :notice) %>
        <% end %>
      <% end %>
    
      <div class="form-group">
        <%= f.label :date, class: "col-sm-2 control-label" %>
        <%= f.date_field :date, class: "form-control" %>
      </div>
    
      <div class="form-group">
        <%= f.label :rationale, class: "col-sm-2 control-label" %>
        <%= f.text_area :rationale, class: "form-control" %>
      </div>
    
      <%= render partial: 'status', locals: {f: f} if current_user.type == 'AdminUser' %>
    
        <%= f.submit 'Save', class: 'btn btn-primary btn-block' %>
    <% end %>
    

    我不知道这里出了什么问题。如果我检查元素,它就在那里:

    enter image description here

    这是我的帖子。rb模型文件:

    class Post < ActiveRecord::Base
      enum status: {submitted: 0, approved: 1, rejected: 2}
      belongs_to :user
      validates_presence_of :date, :rationale
    
      scope :posts_by, ->(user) {where(user_id: user.id)}
    end
    

    昨天考试通过了,不知道发生了什么。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Daniel    7 年前
    1. 你在打电话 login_as 具有 @user 但是 @用户 从未定义,你可能想要 login_as(user) .
    2. 总是试着回顾你的步骤,如果之前有什么东西在工作,想想你在最后一次工作到开始失败之间引入了什么代码。
    3. 检查状态代码对于功能测试来说通常是一种难闻的气味,只需检查用户可以看到的东西即可。
    4. 不要使用 eq 匹配器 current_path 而是使用 have_current_path 匹配器由提供 Capybara .