Search code examples
ruby-on-railsrspecrspec-railsruby-on-rails-7

Capybara::ElementNotFound: Unable to find field "post[date]" that is not disabled


I am getting errors while doing integration tests with rspec and the problem here lies with the fill_in , can anyone explain me why this is happening??

require 'rails_helper'

describe 'navigate' do

  describe "index" do
    before do
      @user = User.create(email:"test@test.com", password:"test123", password_confirmation:"test123", first_name:"Umair", last_name:"Ahmed")
      
    end

    it "can be reached successfully" do
        visit posts_path
        expect(page.status_code).to eq(200)      
    end

  end

  describe "new and create" do
    before do
      user = User.create(email:"test@test.com", password:"test123", password_confirmation:"test123", first_name:"Umair", last_name:"Ahmed")
      login_as(user, scope: user)
      visit new_post_path
    end

    it "has a new form" do
      expect(page.status_code).to eq(200)
    end

    it "will fill in details of form" do
      fill_in "post[date]", with: Date.today
      fill_in "post[rationale]",    with: "something rationale"  

      click_on "Save"

      expect(page).to have_content("something 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.first.posts.first.rationale).to eq("User Association")  
      
    end
    
    
  end
  
  
end

post.rb (model)

class Post < ApplicationRecord
    belongs_to :user, optional: true
    validates_presence_of :date, :rationale
end

views/posts/new.html.erb

<%= form_for @post do |f| %>
    <%= f.date_field :date %>
    <%= f.text_area :rationale %>

    <%= f.submit "Save" %>
<% end %>

I can't seem to understand what is wrong here, can anyone point out what rubbish I am doing here?


Solution

  • UPDATE: I fixed the issue by just changing login_as(user, scope: user) to login_as(user, scope: :user) , or we can type like this if you like this type of syntax login_as(user, :scope => :user)