Rspec功能宏不会工作

时间:2018-06-03 09:28:20

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

我为create_post_spec.rb创建了一个宏 rails v-5.2 ruby v-2.5.1 capybara v-3.2'

我的宏

规格/支持/特征/ session.rb

module Features

  def sign_in(user)
    visit new_user_session_path
    fill_in "Email", with: user.email
    fill_in "Password", with: user.password
    click_on "Log in"
  end

end

然后包含在我的rails_helper

Rspec.confifure do |config|
 config.include Feature, type: feature
end

在我的

规格/特征/ create_post_spec.rb

require "rails_helper"

RSpec.describe“创建帖子”做

let(:user){ User.create(email: "example@mail.com", password: "password",
                     password_confirmation: "password")} 

  scenario "successfuly creating post" do    
    sign_in user
    visit root_path
    click_on "Create post"
    fill_in "Title", with: "Awesome title"
    fill_in "Body", with: "My rspec test"
    click_on "Publish"
    expect(page).to have_current_path root_path
  end

  scenario "unsuccessful creating post" do
    sign_in user
    visit root_path
    click_on "Create post"
    fill_in "Title", with: "Awesome title"
    fill_in "Body", with: ""
    click_on "Publish"
    expect(page).to have_css ".error"  
  end

  scenario "non-logged in user cant create post" do

  end

end

我得到一个未定义的方法sign_in, 但如果我在我的块中使用“功能”

RSpec.feature "Create post....." do

它有效

我想知道为什么如果我使用“describe”

它将不起作用
RSpec.describe "Create post....." do

1 个答案:

答案 0 :(得分:1)

RSpec.featureRspec.describe之间的区别在于RSpec.feature向块添加了type: :featurecapybara_feature: true元数据。重要的是type: :feature元数据,因为它是您用来触发模块包含的内容。您可以通过添加自己的元数据

来使用describe
RSpec.describe "Create post", type: :feature do
  ...
end

或者您可以让RSpec根据spec文件所在的目录自动添加类型,方法是将文件目录更改为spec/features/xxx.rb(注意复数features)并确保

RSpec.configure.do |config|
  config.infer_spec_type_from_file_location!
end
您的rails_helper中已启用

- 请参阅https://relishapp.com/rspec/rspec-rails/docs/directory-structure