ruby-on-rails - Rspec 功能宏不起作用
问题描述
我为我的 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
有用
我想知道如果我使用“描述”它为什么不起作用
RSpec.describe "Create post....." do
解决方案
RSpec.feature
和之间的区别在于Rspec.describe
将RSpec.feature
元数据添加type: :feature
到capybara_feature: true
块中。重要的是type: :feature
元数据,因为它是您用来触发模块包含的东西。您可以describe
通过添加自己的元数据来使用
RSpec.describe "Create post", type: :feature do
...
end
或者您可以通过将文件目录更改为spec/features/xxx.rb
(注意复数features
)并确保RSpec 根据 spec 文件所在的目录自动添加类型
RSpec.configure.do |config|
config.infer_spec_type_from_file_location!
end
在您的 rails_helper 中启用 - 请参阅https://relishapp.com/rspec/rspec-rails/docs/directory-structure
推荐阅读
- python - 在 Plotly 中使用三个 1D 浮点数组创建 3D 曲面图
- javascript - 是什么导致 React.js 上的表单解析错误
- reactjs - 创建反应应用程序 - 小型 EC2 服务器上的 `yarn build` 占用 100% CPU 并使其无响应
- c++ - 树形图的直径
- python - 从经度和纬度获取国家名称
- amazon-web-services - 将事件数据从 Amazon EventBridge 传递到 AWS Fargate 任务
- swift - 如何创建用户可以在其上添加精确点的 3d 地球?
- python - 在 netCDF4 Python 中使用 date2num 转换日期时间时出错
- json - System.Text.Json.JsonSerializer.Serialize 将返回字符串括在双引号中
- linq - 将 if 和 foreach 语句转换为 linq 中的 select 和 where