简体   繁体   English

在 Rails 控制台中执行 rspec 测试

[英]Execute an rspec test in Rails Console

Say I have a user_spec.rb for my User model, and I want to run that test inside the rails console.假设我的用户 model 有一个 user_spec.rb,我想在 rails 控制台中运行该测试。

My first thought is to execute the usual shell command:我的第一个想法是执行通常的 shell 命令:

exec("./spec/user_spec.rb")

But is there a simpler way to run the spec?但是有没有更简单的方法来运行规范? I'm trying to automate some of the tests (and reinvent the wheel a little, yes), so being able to trigger an rspec test inside of another Ruby class seems ideal.我正在尝试自动化一些测试(并且稍微重新发明轮子,是的),因此能够在另一个 Ruby class 内部触发 rspec 测试似乎很理想。

Edit:编辑:

output = `./spec/user_spec.rb`

This will provide the rspec output and $?.success?这将提供 rspec output 和 $?.success? will then provide a pass fail value.然后将提供一个通过失败值。 Is this the best solution here?这是这里最好的解决方案吗? Or is there a way to call an RSpec class itself?或者有没有办法自己调用 RSpec class ?

As pointed out by Anthony in his comment , you can use RSpec::Core::Runner to basically invoke the command line behavior from code or an interactive console.正如Anthony他的评论中指出的那样,您可以使用RSpec::Core::Runner基本上从代码或交互式控制台调用命令行行为。 However, if you use something like Rails, consider that your environment is likely going to be set to development (or even production, if this is where you'll execute the code).但是,如果您使用 Rails 之类的东西,请考虑将您的环境设置为开发环境(甚至生产环境,如果您将在此处执行代码)。 So make sure that whatever you do doesn't have any unwanted side-effects.因此,请确保无论您做什么,都不会产生任何不需要的副作用。

Another thing to consider is that RSpec globally stores its configuration including all example groups that were registerd with it before.另一件要考虑的事情是 RSpec 全局存储其配置,包括之前向其注册的所有示例组。 That's why you'll need to reset RSpec between subsequent runs.这就是您需要在后续运行之间重置 RSpec 的原因。 This can be done via RSpec.reset .这可以通过RSpec.reset来完成。

So putting it all together, you'll get:所以把它们放在一起,你会得到:

require 'rspec/core'
RSpec::Core::Runner.run(['spec/path/to_spec_1.rb', 'spec/path/to_spec_2.rb'])
RSpec.reset

The call to RSpec::Core::Runner.run will output to standard out and return the exit code as a result ( 0 meaning no errors, a non-zero exit code means a test failed).RSpec::Core::Runner.run的调用将输出到标准输出并作为结果返回退出代码( 0表示没有错误,非零退出代码表示测试失败)。

..

Finished in 0.01791 seconds (files took 17.25 seconds to load)
2 example, 0 failures

=> 0

You can also pass other IO objects to RSpec::Core::Runner.run to specify where it should output to.您还可以将其他IO对象传递给RSpec::Core::Runner.run以指定它应该输出到的位置。 And you can also pass other command line parameters to the first array of RSpec::Core::Runner.run , eg '--format=json' to output the results in JSON format.您还可以将其他命令行参数传递给RSpec::Core::Runner.run的第一个数组,例如'--format=json'以 JSON 格式输出结果。

So if you for example want to capture the output in JSON format to then further do something with it, you could do the following:因此,例如,如果您想以 JSON 格式捕获输出,然后对其进行进一步处理,您可以执行以下操作:

require 'rspec/core'

error_stream = StringIO.new
output_stream = StringIO.new

RSpec::Core::Runner.run(
  [
    'spec/path/to_spec_1.rb',
    'spec/path/to_spec_2.rb',
    '--format=json'
  ],
  error_stream,
  output_stream
)

RSpec.reset

errors =
  if error_stream.string.present?
    JSON.parse(error_stream.string)
  end

results =
  if output_stream.string.present?
    JSON.parse(output_stream.string)
  end

Run bundle exec rspec to run all tests or bundle exec rspec./spec/user_spec.rb to run the specific test运行bundle exec rspec运行所有测试或bundle exec rspec./spec/user_spec.rb运行特定测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM