首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Post[date]::ElementNotFound:无法找到未禁用的可见字段“”

Post[date]::ElementNotFound:无法找到未禁用的可见字段“”
EN

Stack Overflow用户
提问于 2017-12-28 16:40:10
回答 1查看 1.2K关注 0票数 0

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

代码语言:javascript
复制
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/_form.html.erb文件:

代码语言:javascript
复制
<%= 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 %>

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

这是我的post.rb模型文件:

代码语言:javascript
复制
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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-28 18:58:50

  1. 您正在用@user调用@user,但是@user从来没有定义过,您可能需要login_as(user)
  2. 总是尝试着回溯你的步骤,如果有什么东西以前起作用了,想想在上次工作到它开始失败之间你引入了什么代码。
  3. 检查状态代码通常是特性测试的难闻之处,只需检查用户可以看到的内容。
  4. 不要将eq匹配器与current_path一起使用,而是使用Capybara提供的have_current_path匹配器。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48011122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档