Faisal
Faisal

Reputation: 149

Capybara method is unacceptable from the calss undefined method `expect' for #<ArticleForm:0xb5e98bc>

The method "expect" it not accessible when trying to call from a class method. However, it works fine when calling from the feature spec.So basically the last method (i_expect_to_see_article_on_home_page) is not working.

spec/support/ArticleForm.rb

    require 'rails_helper'

class ArticleForm
  include Capybara::DSL


  def visit_home_page
    visit('/')
    self
  end

  def create_an_article
    click_on('New Article')
    fill_in('Title', with: "My title")
    fill_in('Content', with: "My content")
    click_on('Create Article')
    self
  end

  def i_expect_to_see_article_on_home_page
    visit('/')
    expect(page).to have_text("My title")
    expect(page).to have_text("My content")
    self
  end
end

spec/features/article_spec.rb

 require 'rails_helper'

require_relative '../support/ArticelForm.rb'
feature "home page" do
  article_form = ArticleForm.new

  scenario  "Visit the home page and post an article" do
    article_form.visit_home_page.create_an_article
    article_form.visit_home_page.i_expect_to_see_article_on_home_page
  end
end

Upvotes: 0

Views: 170

Answers (2)

Thomas Walpole
Thomas Walpole

Reputation: 49950

You need to include RSpec::Matchers in your object. You will probably also need to do the same with Capybara::RSpecMatchers if you want to use the capybara matchers

Upvotes: 1

Lukasz Muzyka
Lukasz Muzyka

Reputation: 2783

Im not familiar with the syntax you're using but I believe you need to wrap it into "it block".This is how I normally write it:

describe "i_expect_to_see_article_on_home_page" do
  it 'should do something' do
    visit('/')
    expect(page).to have_text("My title")
    expect(page).to have_text("My content")
  end
end

Upvotes: 0

Related Questions