设置 rspec 时出现迁移错误 [英] Migration Error When setting up rspec

查看:45
本文介绍了设置 rspec 时出现迁移错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 rspec 测试添加到我已经在开发的应用程序中.我一直在关注本教程:http:///everydayrails.com/2012/03/19/testing-series-rspec-models-factory-girl.html 这是从 2012 年开始的,所以我确定它是使用 Rails 3 完成的.我安装了 rspec 和水豚,运行 bundle,然后运行 ​​rails g rspec:install.我开始在下面编写我的问题模型的测试,当我使用 rspec spec/models/question_spec.rb 运行它时

I'm trying to add rspec testing to an app I've already been working on. I've been following this tutorial: http://everydayrails.com/2012/03/19/testing-series-rspec-models-factory-girl.html which is from 2012, so I'm sure it was done using Rails 3. I installed rspec and capybara, ran bundle, and ran rails g rspec:install. I started writing the test of my Question model below, and when I ran it with rspec spec/models/question_spec.rb

我收到错误消息:迁移正在等待;运行bin/rake db:migrate RAILS_ENV=test"来解决这个问题.我尝试运行它,但收到有关我以前迁移之一的错误:

I received the error: Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=test' to resolve this issue. I tried to run that and I received this error about one of my previous migrations:

==  ChangeTestTypeInScores: migrating =========================================
-- change_column(:scores, :test_type, "boolean USING CAST(test_type AS boolean)")
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: near "USING": syntax error: CREATE TABLE "scores" ("id" INTEGER     PRIMARY KEY AUTOINCREMENT NOT NULL, "test_type" boolean USING CAST(test_type AS boolean),     "name" varchar(255), "created_at" datetime, "updated_at" datetime, "user_id" integer, "month"     varchar(255), "year" varchar(255)) 

如何在不弄乱我的数据库的情况下正确设置 rspec,否则它可以正常工作?

How can I correctly set up rspec without messing up my database, which works fine otherwise?

rails_helper.rb:

rails_helper.rb:

ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'

require 'capybara/rspec'
ActiveRecord::Migration.check_pending!

.rspec:

--color
--require spec_helper
--require rails_helper
--format documentation

question_spec.rb:

question_spec.rb:

require 'spec_helper'

describe Question do
  it "has a valid factory"
  it "is invalid without a body"
  it "is invalid without an answer"
end

我认为根据该错误将 Scores 模型中的 :test_type 更改为 boolean 存在问题,但它在本地和 Heroku 上都运行良好,因此我不想为了运行而弄乱我的数据库测试.任何帮助表示赞赏.

I assume there's an issue with my changing the :test_type in my Scores model to boolean based on that error, but it all works great locally and on Heroku, so I don't want to mess with my database in order to run tests. Any help is appreciated.

更新:我将此添加到 test.rb 文件中:

UPDATE: I added this to the test.rb file:

config.active_record.maintain_test_schema = false

此外,在看到许多其他相关的 Stack Overflow 问题后,我将 Rails 版本更新到了 4.1.6.现在我收到此错误:

Also, I updated my version of rails to 4.1.6 after seeing many other related Stack Overflow issues. Now I get this error:

/Users/tambe257/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:241:in `load': cannot load such file -- /Users/tambe257/programming/rails_projects/fast_track/spec/models/question.rb (LoadError)

看起来某些依赖项有问题,但我一直在谷歌上搜索错误,但没有运气.

It looks like a problem with some dependencies, but I've been googling the error with no luck.

推荐答案

在上面的更新中,我将这个添加到 test.rb 文件中,这实际上清除了:

On the update above I added this to the test.rb file, which actually cleared things up:

config.active_record.maintain_test_schema = false

然后我收到了 (LoadError),但这是因为我的文件名不完全正确.当您输入如下内容时,请确保路径和文件名正确!

I was then getting the (LoadError), but that was due to my file name not being completely correct. When you enter something like this below, be sure the route and file name are correct!

rspec spec/models/question_spec.rb

这篇关于设置 rspec 时出现迁移错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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