生成规范时:
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
最佳答案
是的。我还没有为我的 Controller 做过,但我已经为模型做过了。应该是一样的过程。对于我的模型,我创建了包含以下内容的 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
如果您这样做,请发布您的解决方案。这也让我很烦,所以不介意接受你的版本:-)
关于ruby-on-rails - 我可以让 RSpec 生成具有 expect 语法的规范吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19608197/