我为create_post_spec.rb创建了一个宏 rails v-5.2 ruby v-2.5.1 capybara v-3.2'
我的宏
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
在我的
中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
答案 0 :(得分:1)
RSpec.feature
和Rspec.describe
之间的区别在于RSpec.feature
向块添加了type: :feature
和capybara_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