将 Cucumber 与静态数据库结合使用

发布于 2024-10-02 20:37:26 字数 2559 浏览 2 评论 0原文

我正在尝试对已填充数据的数据库运行 Cucumber 测试。它基本上是我们生产数据库的副本。

由于某种原因,我的一些测试失败了,这似乎是因为它找不到数据。知道如何使这项工作以及为什么它不喜欢“预种子”数据库吗?

我们的数据太复杂,无法尝试通过工厂/装置重新创建。就是没有办法。

这是一个例子:

Scenario: a tech logs in                                                                             
  Given I am on the login page                                                                  
  And user with id 2328 exists                                                                 
  When I login as "user" with password "password"                                            
  Then I should be on the dashboard page                                                             
  And I should see "Logged in as: USER" 

步骤:

Given /^user with id (\d+) exists$/ do |id|
  Employee.exists? id
end


When /^I login as "([^\"]*)" with password "([^\"]*)"$/ do |username, password|
  visit login_url
  fill_in "username", with: username 
  fill_in "pwd", with: password
  click_button "Login"
end

也许是我的步骤,与数据库无关,但我无法理解为什么当我执行 And user with id 2328 isn't 步骤时它没有失败给它一个错误的数字。

我见过这个,但似乎没有来影响流量。

最后,这是实际的登录方法(相信我,我知道这个登录检查有多糟糕,只是检查我们是否拥有数据库的权限):

def login
    if request.post?
      # get database config for env
      configs = ActiveRecord::Base.configurations[Rails.env]
      begin
        # attempt to connect to the db
        plsql = plsql_connect(params[:username], params[:pwd])

        # if no exception, get the employee ID and location
        session[:employee_id] = plsql.hes.installer_web_pkg.get_employee_pk.to_i
        session[:employee_location] = plsql.hes.installer_web_pkg.fn_get_truck_location(session[:employee_id]).to_i

        # logout
        plsql.logoff

        # dashboard time!
        redirect_to dashboard_url(session[:employee_id].to_i)
      rescue Exception => err
        # could not connect, fail!!
        flash.now[:notice] = "Username/password not valid (or locked out of account). Try again or contact the HelpDesk."
        render :action => "login"
      end
    end
  end

每次运行时,我都会得到:

expected: "/dashboard",
     got: "/login" (using ==) (RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/web_steps.rb:198:in `/^(?:|I )should be on (.+)$/'
features/tech_queue.feature:11:in `Then I should be on the dashboard page'

I am trying to run Cucumber tests against a database that is already populated with data. It's basically a copy of our production database.

For some reason I have some tests failing which seemingly is coming from the fact that it can't find the data. Any idea on how to make this work and why it wouldn't like "pre-seeded" databases?

Our data is far too complex to try to recreate via factories/fixtures. There's just no way.

Here's an example:

Scenario: a tech logs in                                                                             
  Given I am on the login page                                                                  
  And user with id 2328 exists                                                                 
  When I login as "user" with password "password"                                            
  Then I should be on the dashboard page                                                             
  And I should see "Logged in as: USER" 

The step:

Given /^user with id (\d+) exists$/ do |id|
  Employee.exists? id
end


When /^I login as "([^\"]*)" with password "([^\"]*)"$/ do |username, password|
  visit login_url
  fill_in "username", with: username 
  fill_in "pwd", with: password
  click_button "Login"
end

Maybe its my steps and has nothing to do with the database, but I can't understand why it doesn't fail on the And user with id 2328 exists step when I give it a bad number.

I have seen this but it doesn't seem to affect the flow.

Lastly, here is the actual login method (believe me, I know how awful this login check is, simply checking we have permissions to the database):

def login
    if request.post?
      # get database config for env
      configs = ActiveRecord::Base.configurations[Rails.env]
      begin
        # attempt to connect to the db
        plsql = plsql_connect(params[:username], params[:pwd])

        # if no exception, get the employee ID and location
        session[:employee_id] = plsql.hes.installer_web_pkg.get_employee_pk.to_i
        session[:employee_location] = plsql.hes.installer_web_pkg.fn_get_truck_location(session[:employee_id]).to_i

        # logout
        plsql.logoff

        # dashboard time!
        redirect_to dashboard_url(session[:employee_id].to_i)
      rescue Exception => err
        # could not connect, fail!!
        flash.now[:notice] = "Username/password not valid (or locked out of account). Try again or contact the HelpDesk."
        render :action => "login"
      end
    end
  end

Every time I run, I get:

expected: "/dashboard",
     got: "/login" (using ==) (RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/web_steps.rb:198:in `/^(?:|I )should be on (.+)$/'
features/tech_queue.feature:11:in `Then I should be on the dashboard page'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文