Capybara方法是不可接受的calss undefined方法`expect&#39;对于#<articleform:0xb5e98bc>

时间:2016-06-27 08:17:02

标签: ruby-on-rails ruby rspec capybara rspec-rails

方法&#34;期待&#34;尝试从类方法调用时无法访问它。但是,从功能规范调用时它工作正常。所以基本上最后一个方法(i_expect_to_see_article_on_home_page)不起作用。

规格/支持/ 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

规格/特征/ 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

2 个答案:

答案 0 :(得分:1)

您需要在对象中include RSpec::Matchers。如果你想使用水豚匹配器,你可能还需要对Capybara :: RSpecMatchers做同样的事情

答案 1 :(得分:0)

我不熟悉您正在使用的语法,但我相信您需要将其包装成“阻止”。这就是我通常写的方式:

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