rspec 核心 Ruby Rails
rspec core Ruby on Rails
您好,我正在使用 rspec:
测试我的应用程序
这是测试文件。
require 'spec_helper'
describe RecipesController do
render_views
describe "index" do
before do
Recipe.create!(name: "Spaghetti alla Carbonara")
Recipe.create!(name: "Spaghetti alle vongole e cozze")
Recipe.create!(name: "Bistecca")
Recipe.create!(name: "Fritto")
xhr :get, :index, format: :json, keywords: keywords
end
subject(:results) { JSON.parse(response.body) }
def extract_name
->(object){ object["name"] }
end
context 'quando la ricerca riporta dei risultati' do
let(:keywords) { 'Spaghetti' }
it 'essere 200' do
expect(response.status).to eq(200)
end
it 'deve ritornare due risultati' do
expect(results.size).to eq(2)
end
it 'deve esserci la ricetta Spaghetti alla Carbonara' do
expect(results.map(&extract_name)).to include('Spaghetti alla Carbonara')
end
it 'deve esserci la ricetta Spaghetti alle vongole e cozze ' do
expect(results.map(&extract_name)).to include('Spaghetti alle vongole e cozze')
end
end
context 'quando la ricerca non riporta alcun risulato' do
let(:keywords) { 'tortellini' }
it 'non deve ritornare alcun risulato' do
expect(results.size).to eq(0)
end
end
end
end
当我尝试通过命令启动测试时:
rake db:migrate RAILS_ENV=test ; rspec spec/controllers/recipes_controller_spec.rb
我收到这个错误
/var/lib/gems/2.1.0/gems/bundler-1.10.1/lib/bundler/runtime.rb:34:in block in setup': You have already activated rspec-core 3.3.1, but your Gemfile requires rspec-core 2.99.2. Prepending
bundle exec` to your command may solve this. (Gem::LoadError)
如日志中所述,错误与 rspec-core v3.3.1 有一些冲突,所以在我的 gem 文件中我添加了这一行以安装 rspec -核心 v2.99.2
gem 'rspec-core', '~> 2.99.2'
但如果我再次启动该命令(在捆绑包安装之后并使用捆绑包执行),结果是一样的。有人有类似的问题吗?
你尝试了吗?
bundle exec rake db:migrate RAILS_ENV=test
bundle exec rspec spec/controllers/recipes_controller_spec.rb
您好,我正在使用 rspec:
测试我的应用程序这是测试文件。
require 'spec_helper'
describe RecipesController do
render_views
describe "index" do
before do
Recipe.create!(name: "Spaghetti alla Carbonara")
Recipe.create!(name: "Spaghetti alle vongole e cozze")
Recipe.create!(name: "Bistecca")
Recipe.create!(name: "Fritto")
xhr :get, :index, format: :json, keywords: keywords
end
subject(:results) { JSON.parse(response.body) }
def extract_name
->(object){ object["name"] }
end
context 'quando la ricerca riporta dei risultati' do
let(:keywords) { 'Spaghetti' }
it 'essere 200' do
expect(response.status).to eq(200)
end
it 'deve ritornare due risultati' do
expect(results.size).to eq(2)
end
it 'deve esserci la ricetta Spaghetti alla Carbonara' do
expect(results.map(&extract_name)).to include('Spaghetti alla Carbonara')
end
it 'deve esserci la ricetta Spaghetti alle vongole e cozze ' do
expect(results.map(&extract_name)).to include('Spaghetti alle vongole e cozze')
end
end
context 'quando la ricerca non riporta alcun risulato' do
let(:keywords) { 'tortellini' }
it 'non deve ritornare alcun risulato' do
expect(results.size).to eq(0)
end
end
end
end
当我尝试通过命令启动测试时:
rake db:migrate RAILS_ENV=test ; rspec spec/controllers/recipes_controller_spec.rb
我收到这个错误
/var/lib/gems/2.1.0/gems/bundler-1.10.1/lib/bundler/runtime.rb:34:in
block in setup': You have already activated rspec-core 3.3.1, but your Gemfile requires rspec-core 2.99.2. Prepending
bundle exec` to your command may solve this. (Gem::LoadError)
如日志中所述,错误与 rspec-core v3.3.1 有一些冲突,所以在我的 gem 文件中我添加了这一行以安装 rspec -核心 v2.99.2
gem 'rspec-core', '~> 2.99.2'
但如果我再次启动该命令(在捆绑包安装之后并使用捆绑包执行),结果是一样的。有人有类似的问题吗?
你尝试了吗?
bundle exec rake db:migrate RAILS_ENV=test
bundle exec rspec spec/controllers/recipes_controller_spec.rb