测试单位:sidekiq worker不查看测试数据库

时间:2015-03-06 22:55:26

标签: ruby-on-rails testing sidekiq

我正在尝试学习TestUnit测试 - 所以这个问题可能很愚蠢。提前抱歉......

我有一个名为PathState的模型,它有一个execute方法,它调用sidekiq worker来完成一些工作。 sidekiq worker接收Path​​State对象id,然后查找PathState,然后尝试执行path_state.action(字符串)。当我运行测试时,我总是看到:

Couldn't find PathState with 'id'=980190977

在测试数据库中存在id 。但看起来sidekiq正在开发数据库中寻找它。

我尝试使用-e test启动sidekiq,但它仍然没有查看测试数据库。

我做错了什么?

这是database.yml:

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: rails
  password: secret
  socket: /var/lib/mysql/mysql.sock

development:
  <<: *default
  database: raws_development

test: &test
  <<: *default
  database: raws_test

production:
  <<: *default
  database: raws_production
  username: raws
  password: <%= ENV['RAWS_DATABASE_PASSWORD'] %>

这是有问题的测试:

  test "should execute action when executed" do
    ps = PathState.create(:name => "TestName", :action => "touch/tmp/PathStateTest")
    ps.execute
    result = `[ -f /tmp/PathStateTest ] && echo 0 || echo 1`
    assert result==0, "OOPS! PathState doesn't execute action when executed!"
  end

这里是sidekiq工人的#perform:

  def perform(path_state_id)
    ps = PathState.find(path_state_id)

    @result = `#{ps.action} 2>&1`
    unless $?.exitstatus.zero?
      # failed state
      Sidekiq.logger.warn "---> PathStateRunner worker failed action: '#    {ps.action}'"
    end
  end

2 个答案:

答案 0 :(得分:0)

这似乎是他们FAQ中描述的问题。

如果您的功能以某种方式与ActiveRecords after_commit hook 相关联,test_after_commit gem可能会对您有所帮助!

祝你好运!

答案 1 :(得分:0)

通过在support / env.rb中添加适当的require语句,我能够在黄瓜测试中运行我的Sidekiq工作人员:

require 'sidekiq/testing'

然后我写了一个看起来像这样的功能:

When I do something which adds a job to a queue
Then I should see "Your jobs were added to the queue."
When I wait for the jobs to complete
Then the completed jobs are listed on the page

现在,当作业到达场景中的第3行时,作业将被排队并执行。 它的定义应该是,

When(/^I wait for the jobs to finish$/) do
  MySpecialWorker.drain
end

此外,所有作业都将针对测试数据库运行