Tag: unit testing

从Association创建的重复记录

我正在使用Mongoid,Rails和Fabrications,并完全失去了这种情况。 任何想法都非常感激,但我知道这很复杂。 我只想制作一个用户并且只有四个已加入的组,但我一直在加载八个。 这是我的代码的相关部分 @ user1 = Fabricate.build(:已注册) @ user1.joined_groups <“Gang of 13”,:type =>:custom})] 当我运行@user1.joined_groups.size我得到4,但当我执行@user1.joined_groups.map(&:name) ,我得到8条记录: # # # # # # # # (我用一个替换了所有BSON :: ObjectId(’4eab3ca5f11aac2701000009’)语句并删除了很多中间代码。 完整的代码集可在此处获取: https : //gist.github.com/1323984 大多数bizzarre只是调用map可能会导致问题。 将“刚刚创建的用户放在这些组中:” puts @ user1.joined_groups.map(&:name) 放“然后” puts @ user1.joined_groups.map(&:name) 生成这个(!): 刚刚创建了这些组的用户: 丹科尔 CA CA46 帮派13 其次是 丹科尔 CA CA46 帮派13 丹科尔 CA CA46 帮派13 […]

应该是rspec匹配器:on =>:create

我正在使用一些Shoulda rspec匹配器来测试我的模型,其中一个是: describe Issue do it { should_not allow_value(“test”).for(:priority) } end 我的问题是我的模型中的validation如下所示: validates_format_of :priority, :with => /^(Low|Normal|High|Urgent)$/, :on => :update 因此,在运行此测试时,我得到: 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) […]

Rails Arelunit testing错误

我有以下错误: ERROR[“test_character_should_be_valid”, CharacterTest, 0.214787] test_character_should_be_valid#CharacterTest (0.21s) NoMethodError: NoMethodError: undefined method `val’ for “$1”:Arel::Nodes::BindParam app/models/user.rb:11:in `block in ‘ test/models/character_test.rb:8:in `setup’ 从以下测试:test / models / character_test.rb: require ‘test_helper’ class CharacterTest < ActiveSupport::TestCase def setup @user = User.new(name: "Example User", email: "user@example.com", callsign: "example", password: "foobar", password_confirmation: "foobar") @user.save @character = @user.character end test "character should be valid" […]

将测试数据存储在文件中的最佳格式是什么?

在我们的项目中,我们使用存储在.sql文件中的测试数据,我们有时会调用这些文件来使用这些测试数据重新创建数据库。 在这个文件中,我们存储如下数据: Insert into Table1 (‘col1’, ‘col2’) values (‘val1’, ‘val2’); Insert into Table1 (‘col1’, ‘col2’) values (‘val3’, ‘val2’); Insert into Table1 (‘col1’, ‘col2’) values (‘val4’, ‘val2’); … 问题是当我们添加或删除列时 ,我们必须遍历所有文件(在我的情况下,有许多大文件)并手动添加列和相应的值:这是大脑中的巨大痛苦。 所以我在想,其他人如何存储他们的测试数据? 我认为使用.yml文件(键值对)的rails方式是好方法,但是如何在非ruby-rails环境中使用它? 例如在rails中: david: name: David Heinemeier Hansson birthday: 1979-10-15 profession: Systems development steve: name: Steve Ross Kellock birthday: 1974-09-27 profession: guy with keyboard

Rails路由问题与控制器测试

我不知道我在这里做错了什么。 任何人有任何见解? 这是我的routes.rb resources :accounts do collection do get “search/:term/:offset/:limit.:format”, :action => “search”, :constraints => { :offset => /\d+/, :limit => /\d+/ } end end 这是我的佣金路线输出… GET /accounts/search/:term/:offset/:limit.:format {:offset=>/\d+/, :action=>”search”, :controller=>”accounts”, :limit=>/\d+/} 这是我的测试线…… get :search, :term => “Test”, :offset => 0, :limit => 2 这是我的错误…… ActionController::RoutingError: No route matches {:term=>”Test”, :action=>”search”, :controller=>”accounts”, :offset=>0, :limit=>2} 有任何想法吗? […]

使用Minitest测试自定义validation器

我有多个模型与电子邮件validation。 因此,我已将validation提取到自定义validation器中。 我按照Rails指南的教程来说明这一点。 class EmailValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[az]{2,})\z/i record.errors[attribute] << (options[:message] || "is not an email") end end end 到现在为止还挺好。 但是,由于我已经将电子邮件validation的function提取到了自己的范围内,我还想单独测试它。 我不想为每个模型添加相同的电子邮件格式测试。 我发现了另一个问题 ,但也对RSpec提出了同样的问题 。 但由于我还没有使用存根和模拟,我不知道如何将测试移植到Minitest测试。 我没有找到任何用Minitest测试自定义validation器的资源。 有人知道如何在Minitest中为自定义validation器编写此类测试(不使用规范!)?

如何为测试目的模拟数据库故障(在Ruby on Rails中)

通过某些监视工具(例如Monit)使用心跳消息使应用程序幸存下来是一种常见的设置。 如果应用程序正在运行并且一切正常,它将返回“我还活着”消息,如果数据库失败或Web服务器挂起,则返回任何内容或内部服务器错误(HTTP状态代码500)页面。 如何模拟数据库失败以在Ruby on Rails中测试此行为? 如果可以在测试( Test::Unit或RSpec )本身内为测试目的启用/禁用此function,那将是很好的。

应该:测试validates_presence_of:on =>:update

我在我工作的一个项目中使用了Shoulda和Test :: Unit。 我遇到的问题是我最近改变了这个: class MyModel < ActiveRecord::Base validates_presence_of :attribute_one, :attribute_two end 对此: class MyModel :update end 以前,我的(传递)测试看起来像这样: class MyModelTest < ActiveSupport::TestCase should_validate_presence_of :attribute_one, :attribute_two end 据我所知, should_validate_presence_of没有参数会导致此测试继续传递上面指定的更改。 在测试需求:attribute_two ,没有放弃Shoulda,有什么方法可以解决这个问题吗?

是否有可能和/或建议在rails中动态生成测试?

我发现在rails应用程序编程中非常方便的一个技巧是class_eval可用于动态创建方法。 我现在开始进行测试,我想知道是否可以使用类似的想法来生成测试。 例如,我有一个before_filter要求用户登录控制器中的所有操作。 我想编写测试,确保将before_filter应用于所有操作。 我不想单独写出每个测试,而是自动生成所有这些测试。 这种类型的测试是否可取,或者我应该坚持单独编写测试? 如果是的话,怎么会这样做呢? 编辑:这可能看起来像: actions = {:index => :get,:show => :get,:edit => :get,:update => :put} actions.each_pair do |action,type| class_eval(%Q{def test_user_required_for_#{action} set_active_user users(:one) #{type} :#{action} assert flash[:error] assert_redirected_to :action => :index end }) end 现在人们已经证实这可能是有用的,我会在哪里放置一段这样的代码,以便它只执行一次,只创建一次这些测试?

Rails测试 – 如何测试特定的validation?

使用默认的Rails测试套件,我如何编写仅测试某些validation的测试? 例如,如果我有presence,numeric和我自己的validation器的validation,我该如何测试数字? 假设我没有数字validation工作,所以此测试应该失败: test “should test numeric validator” do post = Post.new(:number => ‘not a number’) assert !post.save, “it saved a non-numeric value for :number” end 但它会通过,因为我自己的validation器不允许它出于其他原因保存,所以我的测试没有真正测试。