如何在没有Rails的情况下使用RSpec?

时间:2012-09-06 19:32:21

标签: ruby rspec tdd bdd

使用没有Rails的RSpec在Ruby中执行TDD的过程是什么?

我需要Gemfile吗?它只需要rspec吗?

Ruby 1.9.3

3 个答案:

答案 0 :(得分:64)

流程如下:

从控制台安装rspec gem:

gem install rspec

然后使用以下内容创建一个文件夹(我们将其命名为root):

根/ my_model.rb

根/规格/ my_model_spec.rb

#my_model.rb
class MyModel
  def the_truth
    true
  end
end

#spec/my_model_spec.rb

require_relative '../my_model'

describe MyModel do
  it "should be true" do
    MyModel.new.the_truth.should be_true
  end
end

然后在控制台中运行

rspec spec/my_model_spec.rb

瞧!

答案 1 :(得分:41)

从项目目录中......

gem install rspec
rspec --init

然后在创建的spec目录中编写规范并通过

运行它们
rspec 'path to spec' # or just rspec to run them all

答案 2 :(得分:5)

routes: [ path: '/dashboard', name: 'dashboard', component: DashboardIndex, children: [{ path: 'popup', component: PopupPlaceholder, children: [{ path: 'a', name: 'a', component: A, }, { path: 'b', name: 'b', component: B, }], }], ] 周围的工作流程存在缺陷。始终使用Bundler和Gemfile来确保一致性,避免项目在一台计算机上正常工作但在另一台计算机上失败的情况。

创建gem install rspec

Gemfile

然后执行:

source 'https://rubygems.org/'

gem 'rspec'

以上内容将为您创建gem install bundler bundle install bundle exec rspec --init .rspec

现在在spec/spec_helpers.rb中创建示例规范:

spec/example_spec.rb

运行规范:

describe 'ExampleSpec' do
  it 'is true' do
    expect(true).to be true
  end
end