设置RSpec来测试一个gem(不是Rails) [英] Setup RSpec to test a gem (not Rails)

查看:122
本文介绍了设置RSpec来测试一个gem(不是Rails)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用rspec-rails的添加生成器来设置RSpec以测试Rails应用程序非常简单。但是如何添加RSpec来测试开发中的gem?
我没有使用珠宝商或此类工具。我只是使用Bundler( bundle gem my_gem )来设置新gem的结构并手动编辑* .gemspec。
我还在gemspec中添加了 s.add_development_dependencyrspec,> = 2.0.0并且做了一个 bundle install < code $。

是否有一些不错的教程,接下来要做什么以获得RSpec工作?

解决方案

我已经更新了这个答案以符合当前的最佳实践:Bundler支持gem开发。如果您正在创建一个gem,那么您需要在Gemfile中包含的 only 事件如下:

  sourcehttps://rubygems.org
gemspec

这告诉Bundler当你运行 bundle install



时,请在gemspec文件中寻找依赖关系。确保RSpec是宝石的开发依赖。编辑gemspec,使其为:

  spec.add_development_dependencyrspec

接下来,创建 spec / spec_helper.rb 并添加如下内容:

 需要'bundler / setup'
Bundler.setup

需要'your_gem_name'#和其他任何您需要的宝石

RSpec.configure do | config |
#some(可选)config here
end

前两行告诉Bundler只加载gemspec中的宝石。当您在自己的机器上安装自己的Gem时,这会强制您的规格使用您当前的代码,而不是您单独安装的版本。



创建规格示例 spec / foobar_spec.rb

  require'spec_helper'
描述Foobar do
等待写入
结束

可选:为默认选项添加 .rspec 文件,并将其放入您的gem根路径中:

   -  color 
- 格式文档

最后:运行规格:

  $ rspec spec / foobar_spec.rb 


It is pretty easy with the added generator of rspec-rails to setup RSpec for testing a Rails application. But how about adding RSpec for testing a gem in development? I am not using jeweler or such tools. I just used Bundler (bundle gem my_gem) to setup the structure for the new gem and edit the *.gemspec manually. I also added s.add_development_dependency "rspec", ">= 2.0.0" to gemspec and did a bundle install.

Is there some nice tutorial what to do next to get RSpec working?

解决方案

I've updated this answer to match current best practices:

Bundler supports gem development perfectly. If you are creating a gem, the only thing you need to have in your Gemfile is the following:

source "https://rubygems.org"
gemspec

This tells Bundler to look inside your gemspec file for the dependencies when you run bundle install.

Next up, make sure that RSpec is a development dependency of your gem. Edit the gemspec so it reads:

spec.add_development_dependency "rspec"

Next, create spec/spec_helper.rb and add something like:

require 'bundler/setup'
Bundler.setup

require 'your_gem_name' # and any other gems you need

RSpec.configure do |config|
  # some (optional) config here
end

The first two lines tell Bundler to load only the gems inside your gemspec. When you install your own gem on your own machine, this will force your specs to use your current code, not the version you have installed separately.

Create a spec, for example spec/foobar_spec.rb:

require 'spec_helper'
describe Foobar do
  pending "write it"
end

Optional: add a .rspec file for default options and put it in your gem's root path:

--color
--format documentation

Finally: run the specs:

$ rspec spec/foobar_spec.rb

这篇关于设置RSpec来测试一个gem(不是Rails)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆