Search code examples
ruby-on-railsrspeccapybararspec-rails

Rspec fail missing required keys: [:id]


When I run my spec, I get the following.

enter image description here

Why is that so? I just wanted to make sure that I'm on what should be the show action inside the ProjectsController.

spec

require 'spec_helper'

describe "Creating projects" do
  it "can create a project" do
    visit '/'

    click_link 'New Project'
    fill_in 'Name', with: 'TextMate2'
    fill_in 'Description', with: 'A text-editor for OS X'

    click_button 'Create Project'
    expect(page).to have_content('Project has been created.')

    project = Project.where(name: "TextMate 2").first
    expect(page.current_url).to eql(project_url(project))

    title = "TextMate 2 - Projects - Ticketee"
    expect(page).to have_title(title)
  end

controller

  def show
    @project = Project.find(params[:id])
  end

view

<h2><%= @project.name %></h2>

Solution

  • Change this line

    project = Project.where(name: "TextMate 2").first
    

    to

    project = Project.where(name: "TextMate2").first
    

    You are created a project with name TextMate2 not TextMate 2, so project = Project.where(name: "TextMate 2").first returns nil