ruby-on-rails - 在我的 RSpec 请求规范中调用 sleep 是否有效?

标签 ruby-on-rails testing rspec capybara factory-bot

我正在单击 show and destroy links with Capybara,它应该位于 employees 表的第一行。该表应根据 updated_at 时间戳进行排序,以便最近修改的员工位于顶部。

在示例之前,必须创建一个通过身份验证的有效员工。然后在示例的开头,必须创建一个员工来进行测试。 This 是应该始终位于表顶部的员工,因为它应该是最近修改过的。有时是,有时不是。添加 sleep 调用可以解决此问题,我想知道这是否有效或者我是否弄错了。我认为在测试中添加 sleep 调用是一件坏事。我还认为,如果身份验证员工是在示例之前创建的,那么即使我的规范运行得非常快,它仍然应该有一个更早的 updated_at 时间戳。

身份验证宏:

module AuthenticationMacros
  def login_confirmed_employee
    # create a valid employee for access
    Factory :devise_confirmed_employee,
      :username => 'confirmed.employee',
      :password => 'password',
      :password_confirmation =>'password'

    # sign in with valid credentials
    visit '/employees/sign_in'
    fill_in 'Username', :with => 'confirmed.employee'
    fill_in 'Password', :with => 'password'
    click_on 'Sign In'
  end
end

有问题的请求规范:

require 'spec_helper'
include AuthenticationMacros

describe "Employees" do

  before do
    login_confirmed_employee
  end

  # shows employees
  it "shows employees" do
    sleep 1.seconds
    Factory :employee_with_all_attributes,
      :username => 'valid.employee',
      :email => 'valid.employee@example.com',

    visit '/employees'
    within 'tbody tr:first-child td:last-child' do; click_on 'Show', end
    page.should have_content 'valid.employee'
    page.should have_content 'valid.employee@example.com'
  end


  # destroys employees
  it "destroys employees" do
    sleep 1.seconds
    Factory(:employee)
    visit '/employees'
    within 'tbody tr:first-child td:last-child' do; click_on 'Delete', end
    page.should have_content 'Employee was successfully deleted.'
  end
end 

另外还有我的 spec_helper:

require 'spork'

Spork.prefork do

  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
  ActiveSupport::Dependencies.clear
  require 'email_spec'
  RSpec.configure do |config|
    config.include EmailSpec::Helpers
    config.include EmailSpec::Matchers
  end
end

Spork.each_run do
  require 'rspec/rails'
  require 'factory_girl_rails'
  require 'capybara/rspec'
  require 'capybara/rails'
  require 'shoulda'

  RSpec.configure do |config|

    config.mock_with :rspec
    config.use_transactional_fixtures = false

    config.before(:suite) do
      DatabaseCleaner.strategy = :truncation
    end

    config.before(:each) do
      DatabaseCleaner.start
    end

    config.after(:each) do
      DatabaseCleaner.clean
    end
  end
end

有时我最终会进入显示页面或销毁需要身份验证的员工而不是示例员工。如果我添加 1 秒 sleep 调用,这种情况永远不会发生。

最佳答案

不,事实上这是一个非常糟糕的解决方案,因为它增加了测试执行时间。 您可以使用以下解决方案之一模拟 Time.now 方法调用:

关于ruby-on-rails - 在我的 RSpec 请求规范中调用 sleep 是否有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7637590/

相关文章:

testing - 导入带有可变参数的机器人库

ruby-on-rails - 带有 Rails 2.3.x 的 RSpec-2

ruby-on-rails - 如何测试 before_filter 与 Rails 中的 RSpec 是否正常工作

ruby-on-rails - 如何将 Rails config.time_zone 设置为 GMT +6?

ruby-on-rails - 如何将 session 变量从一个 Rails 3.2.8 引擎传递到另一个引擎?

javascript - UI Bootstrap 破坏 angularjs post 请求

java - 有一个用于用户注册的 junit 测试用例有意义吗?

javascript - 单击具有唯一 ID 的链接上的事件

带接口(interface)的 PHPunit 模拟对象

ruby-on-rails - Rspec 不识别 RSpec::Matcher 和 RSpec::Core 的任何方法