我最近注意到,如果我的测试子类为Test :: Unit :: TestCase,我的测试数据库在运行测试后没有被清理.如果我的测试子类是ActiveSupport :: TestCase,那么所有内容都会被正确清理.
任何人都可以解释为什么,和/或提供使用一个与另一个的解释?
我正在使用shoulda和factory_girl.
谢谢.
我有一个或多或少像这样的测试:
class FormDefinitionTest < ActiveSupport::TestCase
context "a form_definition" do
setup do
@definition = SeedData.form_definition
# ...
Run Code Online (Sandbox Code Playgroud)
我故意添加了一个
raise "blah"
Run Code Online (Sandbox Code Playgroud)
在某个地方,我得到这个错误:
RuntimeError: blah
test/unit/form_definition_test.rb:79:in `__bind_1290079321_362430'
Run Code Online (Sandbox Code Playgroud)
什么时候我应该得到一些东西:
/Users/pupeno/projectx/db/seed/sheet_definitions.rb:17:in `sheet_definition': blah (RuntimeError)
from /Users/pupeno/projectx/db/seed/form_definitions.rb:4:in `form_definition'
from /Users/pupeno/projectx/test/unit/form_definition_test.rb:79
Run Code Online (Sandbox Code Playgroud)
什么是消毒/摧毁我的回溯?我怀疑是应该的,因为异常发生在一个设置中,或应该发生.
这是一个Rails 3项目,如果这很重要的话.
我注意到的一件事是,在我做的大多数项目中,一个需要很长时间(30秒+)的规范就是这个应该/回形针助手:
it { should validate_attachment_content_type(:bannerimage)
.allowing('image/png', 'image/jpeg', 'image/gif', 'image/jpg')
.rejecting('text/plain')
}
Run Code Online (Sandbox Code Playgroud)
我非常想保留内容类型验证,但我想知道是否有更快的方法来实现它.我已经用以下标记标记了这些测试:慢速运行rspec而没有:慢速规范,但是,我希望有人能够更快地测试图像内容类型.
通过回答关于属性可访问性测试的另一个StackOverflow问题(并认为它们非常棒)来了解shoulda-matchers后,我决定尝试重构我在The Rails Tutorial中所做的模型测试,试图使它们更加简洁和彻底.我这样做是由于从文档模块的一些灵感和,以及这个StackOverflow的答案就在模型结构早该测试.但是,还有一些我不确定的事情,我想知道如何使这些测试更好. Shoulda::Matchers::ActiveRecord
Shoulda::Matchers::ActiveModel
我将使用Rails教程中的用户规范作为我的示例,因为它是最详细的,并涵盖了许多可以改进的领域.以下代码示例已从原始user_spec.rb更改,并将代码替换为describe "micropost associations"
行.针对user.rb模型的规范测试及其工厂在factories.rb中定义.
规格/型号/ user_spec.rb
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime not null
# updated_at :datetime not null
# password_digest :string(255)
# remember_token :string(255)
# admin :boolean default(FALSE)
#
# Indexes
#
# index_users_on_email (email) UNIQUE
# index_users_on_remember_token (remember_token) …
Run Code Online (Sandbox Code Playgroud) 我试图找出另一种写应该改变计数测试的方法(没有lambda).我正在使用Rails 3.我也在使用shoulda matcher gem
原因 - 所有测试用例都采用格式
describe "some stuff" do
it { should ... }
end
Run Code Online (Sandbox Code Playgroud)
但我无法按照相同的模式来测试应该更改计数
这就是我所拥有的
describe "some stuff" do
it "should change count by one" do
lambda { ... }.should change(Model, :count).by(1)
end
end
Run Code Online (Sandbox Code Playgroud)
有没有办法写它
describe "some stuff" do
it { should change(Model, :count).by(1) }
end
Run Code Online (Sandbox Code Playgroud)
非常感谢 !!
我有3个型号,用户,游戏和播放器.用户和游戏之间基本上存在多对多的关系,玩家作为联接表,除了玩家有其他信息,因此它有自己的模型.
玩家需要游戏ID和用户ID的唯一组合,所以我试着在玩家中说:
validates_uniqueness_of :user_id, :scope => :game_id
Run Code Online (Sandbox Code Playgroud)
然后在我的规范中,我说(使用shoulda matchers):
it { should validate_uniqueness_of(:user_id).scoped_to(:game_id)}
Run Code Online (Sandbox Code Playgroud)
以下是玩家定义的关系:
belongs_to :game, :inverse_of => :players
belongs_to :user, :inverse_of => :players
Run Code Online (Sandbox Code Playgroud)
但我在该规范上得到一个ActiveRecord :: statementinvalid错误
ActiveRecord::StatementInvalid: Mysql2::Error: Column 'game_id' cannot be null: INSERT INTO `players` ETC...
Run Code Online (Sandbox Code Playgroud)
知道出了什么问题吗?
我正在升级Rails 2到Rails 3应用程序(代码不是由我编写的).(经过良好测试的代码)使用shoulda和Test :: Unit,并广泛使用宏should_create和should_change.
我从这个讨论中了解到,shoulda维护者希望摆脱这两种方法,但使用Test :: Unit的人并不觉得有必要(不确定我是否正在抓住整个讨论).
Anaway,有没有办法选择性地转换指定宏的弃用警告?我已经从这篇文章中了解到,您可以通过设置完全关闭Rake测试输出中的弃用警告:
ActiveSupport::Deprecation.silenced = true
Run Code Online (Sandbox Code Playgroud)
在您的测试环境文件中,我也知道您可以将特定的代码块放在一个块中以使它们静音:
ActiveSupport::Deprecation.silence do
# no warnings for any use of deprecated methods here
end
Run Code Online (Sandbox Code Playgroud)
后者是一个选项,但需要我遍历所有测试并将should_create宏包含在这样的块中.所以我想知道有一种方法可以完全消除特定宏的警告,只需一个配置设置?
我正在测试我的资源控制器的删除操作,如下所示:
describe ResourceController do
context "DELETE destroy" do
before :each do
delete :destroy, id: @resource.id
end
it { should respond_with(:no_content) }
end
end
Run Code Online (Sandbox Code Playgroud)
我期待204 /无内容的回复.但是,此测试失败,因为服务器返回的响应是406.当我直接从我的Rest测试客户端命中控制器时,响应是204.
我正在使用一些Shoulda rspec匹配器来测试我的模型,其中一个是:
describe Issue do
it { should_not allow_value("test").for(:priority) }
end
Run Code Online (Sandbox Code Playgroud)
我的问题是我的模型验证如下:
validates_format_of :priority, :with => /^(Low|Normal|High|Urgent)$/, :on => :update
Run Code Online (Sandbox Code Playgroud)
因此,当运行此测试时,我得到:
1) 'Issue should not allow priority to be set to "test"' FAILED
Expected errors when priority is set to "test", got errors: category is invalid (nil)title can't be blank (nil)profile_id can't be blank (nil)
Run Code Online (Sandbox Code Playgroud)
验证没有被触发,因为它只在更新上运行,我如何在更新和创建时使用这些应该匹配?
我有一个模型
class Certificate < ApplicationRecord
notification_object
belongs_to :user
belongs_to :share_class, optional: true
belongs_to :round, optional: true
belongs_to :company, optional: true
has_many :approvals, as: :votable
end
Run Code Online (Sandbox Code Playgroud)
这个模型的规格看起来像这样
需要'rails_helper'
RSpec.describe Certificate, type: :model do
it { should belong_to(:user) }
it { should belong_to(:share_class).optional }
it { should belong_to(:round).optional }
it { should belong_to(:company).optional }
it { should have_many(:approvals) }
end
Run Code Online (Sandbox Code Playgroud)
但是当我运行这个规范时,我得到了这个错误
1) 证书应属于 share_class 可选:true
Failure/Error: it { should belong_to(:share_class).optional }
Expected Certificate to have a belongs_to association called share_class (the association …
Run Code Online (Sandbox Code Playgroud) shoulda ×10
rspec ×7
ruby ×3
unit-testing ×2
backtrace ×1
deprecated ×1
factory-bot ×1
paperclip ×1
refactoring ×1
rspec-rails ×1
testing ×1