使用Cucumber,有没有一种方法来记录用户没有接口? [英] Using Cucumber, is there a way to log a user in without the interface?

查看:207
本文介绍了使用Cucumber,有没有一种方法来记录用户没有接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的黄瓜功能绝大多数需要用户登录。但是我真的不需要测试每个单一测试的登录功能。我目前正在使用Devise进行身份验证。

The vast majority of my cucumber features require the user to be logged in. However I don't really need to test the login functionality for every single test. I'm currently using Devise for my authentication.

我正在寻找一种使用设计来签署用户的方法,而无需填写登录表单。有没有做到这一点?我希望不必在每次测试中使用sign in action。

I'm looking for a way to sign a user in with devise, without filling out the sign in form. Is there anyway to do this? I would prefer to not have to use the sign in action for every test.

推荐答案

不,没有办法。在文档中,关于 sign_in @user sign_out @user 帮助方法,它说:

No, there is no way. In the documentation, with regard to the sign_in @user and sign_out @user helper methods, it says:


这些帮助程序不工作用于由Capybara或Webrat驱动的集成测试。它们仅用于功能测试。相反,填写表单或在会话中显式设置用户
These helpers are not going to work for integration tests driven by Capybara or Webrat. They are meant to be used with functional tests only. Instead, fill in the form or explicitly set the user in session

正如你自己说的,使用 before:each block。我喜欢如下结构:

As you said yourself, it is probably cleanest to do it with a before :each block. I like to structure it like the following:

context "login necessary" do
  # Before block
  before do
    visit new_user_session_path
    fill_in "Email", with: "test@test.com"
    fill_in "Password", with: "password"
    click_button "Login"
    assert_contain "You logged in successfully."
  end

  # Actual tests that require the user to be logged in
  it "does everything correctly" do
    # ...
  end
end

context "login not necessary" do
  it "does stuff" do
    # code
  end
end

我发现这是非常有用的,因为如果我更改身份验证规则(即用户是否必须登录一个特定的路径)我可以只进行整个测试,并将其移动到其他描述块,而不改变任何更多的代码。

I found this to be quite useful, since if I change authentication rules (i.e. whether or not the user has to be logged in for a specific path) I can just take the whole test and move it into the other description block, without changing any more code.

这篇关于使用Cucumber,有没有一种方法来记录用户没有接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆