如何使用 rspec 测试 (Bean)Stalker?

发布于 2024-10-26 04:57:46 字数 454 浏览 7 评论 0原文

我有这个名为 jobs.rb 的文件

require File.expand_path("../environment", __FILE__)

job "do.stuff" do |args|
  $value = 10
end

然后我有这个名为 jobs_spec.rb 的规范文件

require File.expand_path("../../spec_helper", __FILE__)

describe "some beanstalk jobs" do
  it "should work" do

    # What to do here?

    $value.should eq(10)
  end
end

如何测试 $value 变量?

I've this file called jobs.rb

require File.expand_path("../environment", __FILE__)

job "do.stuff" do |args|
  $value = 10
end

Then I've this spec file called jobs_spec.rb

require File.expand_path("../../spec_helper", __FILE__)

describe "some beanstalk jobs" do
  it "should work" do

    # What to do here?

    $value.should eq(10)
  end
end

How do I test the $value variable?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一绘本一梦想 2024-11-02 04:57:46

您必须在运行测试的机器上运行 beanstalkd。


require "Stalker"

describe Stalker, "job" do
  before :all do
    # Make sure beanstalkd is running
    if `pgrep beanstalkd` == ""
      raise "PRECONDITION NOT MET: beanstalkd not running"
    end

    module Stalker
      def log(msg); end
      def log_error(msg); end
    end
  end

  before :each do
    Stalker.clear!
    $result = -1
    $handled = false
  end

  it "enqueue and work a job" do
    val = rand(999999)
    Stalker.job('my.job') { |args| $result = args['val'] }
    Stalker.enqueue('my.job', :val => val)
    Stalker.prep
    Stalker.work_one_job
    val.should == $result
  end
end

You have to run beanstalkd on the machine you are running the test on.


require "Stalker"

describe Stalker, "job" do
  before :all do
    # Make sure beanstalkd is running
    if `pgrep beanstalkd` == ""
      raise "PRECONDITION NOT MET: beanstalkd not running"
    end

    module Stalker
      def log(msg); end
      def log_error(msg); end
    end
  end

  before :each do
    Stalker.clear!
    $result = -1
    $handled = false
  end

  it "enqueue and work a job" do
    val = rand(999999)
    Stalker.job('my.job') { |args| $result = args['val'] }
    Stalker.enqueue('my.job', :val => val)
    Stalker.prep
    Stalker.work_one_job
    val.should == $result
  end
end
A君 2024-11-02 04:57:46

我会检查库本身如何运行它的测试:

https:// github.com/adamwiggins/stalker/blob/master/test/stalker_test.rb

请记住,这是使用 Test::Unit,但是您可以在 Rspec 中应用相同的技术。

I'd check out how the library itself runs it's test:

https://github.com/adamwiggins/stalker/blob/master/test/stalker_test.rb

Keep in mind that this is using Test::Unit, however you can apply the same techniques in Rspec.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文