mercredi 17 août 2016

Products Model testing Using Rspec and capybara

Product.rb

 class Product < ApplicationRecord
 validates :name, presence: true, uniqueness: true

end

Product_spec.rb

require 'rails_helper'
require 'capybara/rspec'

RSpec.feature "Create Product", :type => :feature do

scenario "User creates a duplicate product" do
    visit "/products/new"

    fill_in "Name", :with => "My Widget"
    click_button "Create"
    #expect(page).to have_content("Name has already been taken")
    #expect(flash[:notice]).to have_content("Name has already been taken")

     expect(flash[:alert]).to match(/Name has already been taken/)
  end

  scenario "User creates a invalid product" do
     visit "/products/new"

     fill_in "Name", :with => ""
     click_button "Create"

     expect(flash[:alert]).to have_text("Name can't be blank.")
  end

 end

Error Displayed : Error

How do I catch the error messages that are raised in Flash ? I tried everything like to have_content , to contain etc

Aucun commentaire:

Enregistrer un commentaire