我能用RSpec生成具有期望语法的规范吗?

时间:2013-10-26 15:05:33

标签: ruby-on-rails rspec

生成规格时使用:

rails g controller Home index

使用旧的object.should语法

生成规范
require 'spec_helper'

describe HomeController do   
  describe "GET 'index'" do
    it "returns http success" do
      get 'index'
      response.should be_success
    end
  end  
end

是否可以将生成器配置为使用expect语法?

期望的输出:

require 'spec_helper'

describe HomeController do

  describe "GET 'index'" do
    it "returns http success" do
      get 'index'
      expect(response).to be_success
    end
  end

end

在config / application.rb中:

config.generators do |g|
  g.test_framework :rspec, fixture: true
  g.fixture_replacement :factory_girl, dir: 'spec/factories'
  g.view_specs false
  g.stylesheets = false
  g.javascripts = false
end

2 个答案:

答案 0 :(得分:3)

是。我没有为我的控制器做过,但我已经为模型做过了。应该是同一个过程。对于我的模型,我使用以下内容创建了lib/templates/rspec/model/model_spec.rb

require 'spec_helper'

describe <%= class_name %> do

  let(:<%= singular_name %>) { FactoryGirl.create(:<%= singular_name %>) }

  it "should be valid from the factory" do
    expect(<%= singular_name %>).to be_valid
  end

end

如果您这样做,请发布您的解决方案。它也让我烦恼,所以不介意你的版本: - )

答案 1 :(得分:0)

在3.0.0.rc1版本中可用。 gem 'rspec', '~> 3.0.0.rc1'