在一个 rspec 测试中键入帮助程序并键入请求
type helper and type request in one rspec test
Rails 5.2.2
当我需要在我的测试中添加助手时,我使用类型助手:
RSpec.describe Utilities, type: :helper do
end
当我需要添加一些请求,如 pach 或 delete 或 post 我使用类型请求:
RSpec.describe 'AuthenticationPages', type:
:request do
end
但是当我需要同时使用类型助手和请求时,代码应该是什么?
示例代码:
RSpec.describe Utilities, type: :helper do
describe 'as wrong user' do
let(:user) {FactoryGirl.create(:user)}
let(:wrong_user) {FactoryGirl.create(:user, email: 'wrong@example.com')}
before {sign_in user, no_capybara: true}
describe 'submitting a GET request to the Users#edit action' do
before {get edit_user_path(wrong_user)}
specify {expect(response.body).not_to match(full_title('Edit user'))}
specify {expect(response).to redirect_to(root_url)}
end
end
type
只是一个元数据,您可以在 documentation 上阅读更多相关信息。使用 Rails 和 Rspec 时,会自动推断类型加载额外的好东西以简化测试编写。
据我了解,无法提供类型列表或类似内容,因此您有几个选择。
选项 1:为您的规范选择一个类型,手动从另一个类型加载您需要的内容。
选项 2:创建一个新的专用类型,加载您需要的所有类型,从 rails_helper.rb
:
中提取的简短示例
RSpec.configure do |config|
# ...
config.include JsonHelpers, type: :my_unique_type
# ...
end
在这种情况下,如果用 type: :my_unique_type
标记一个规范,JsonHelpers
将被加载并在该规范中可用。
选项 3:拆分职责,因此您不需要这两种类型,将助手与请求测试分开测试。
RSpec.describe 'AuthenticationPages', type: :request do
describe Utilities, type: :helper do
//it's work
end
end
Rails 5.2.2
当我需要在我的测试中添加助手时,我使用类型助手:
RSpec.describe Utilities, type: :helper do
end
当我需要添加一些请求,如 pach 或 delete 或 post 我使用类型请求:
RSpec.describe 'AuthenticationPages', type:
:request do
end
但是当我需要同时使用类型助手和请求时,代码应该是什么?
示例代码:
RSpec.describe Utilities, type: :helper do
describe 'as wrong user' do
let(:user) {FactoryGirl.create(:user)}
let(:wrong_user) {FactoryGirl.create(:user, email: 'wrong@example.com')}
before {sign_in user, no_capybara: true}
describe 'submitting a GET request to the Users#edit action' do
before {get edit_user_path(wrong_user)}
specify {expect(response.body).not_to match(full_title('Edit user'))}
specify {expect(response).to redirect_to(root_url)}
end
end
type
只是一个元数据,您可以在 documentation 上阅读更多相关信息。使用 Rails 和 Rspec 时,会自动推断类型加载额外的好东西以简化测试编写。
据我了解,无法提供类型列表或类似内容,因此您有几个选择。
选项 1:为您的规范选择一个类型,手动从另一个类型加载您需要的内容。
选项 2:创建一个新的专用类型,加载您需要的所有类型,从 rails_helper.rb
:
RSpec.configure do |config|
# ...
config.include JsonHelpers, type: :my_unique_type
# ...
end
在这种情况下,如果用 type: :my_unique_type
标记一个规范,JsonHelpers
将被加载并在该规范中可用。
选项 3:拆分职责,因此您不需要这两种类型,将助手与请求测试分开测试。
RSpec.describe 'AuthenticationPages', type: :request do
describe Utilities, type: :helper do
//it's work
end
end