Rspec 功能宏不起作用

Rspec features macro wont work

我为 create_post_spec.rb 创建了一个宏 railsv-5.2 rubyv-2.5.1 水豚 v-3.2'

我的宏

spec/support/features/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

在我的

spec/feature/create_post_spec.rb

require "rails_helper"

RSpec.describe“创建post”做

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

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