Tag: 测试

使用FactoryGirl时,Paperclip :: Attachment作为字符串传递

我正在尝试使用FactoryGirl 3.0在rails 3.2.6上创建回形针3.1.2的function测试,当我传递文件附件时,我的控制器接收一个字符串,它是文件位置而不是Paperclip :: Attachment对象。 我的工厂是: include ActionDispatch::TestProcess FactoryGirl.define do factory :artist do id 1 name ‘MyString’ photo {fixture_file_upload(“#{Rails.root}/test/fixtures/files/rails.png”,’image/png’)} end end 我的测试是: test “should create artist” do assert_difference(‘Artist.count’) do post :create, :artist => { name: @artist.name, photo: @artist.photo }, :html => { :multipart => true } end assert_redirected_to artist_path(assigns(:artist)) end 在我的控制器中: params[:artist][:photo].class.name 等于“字符串”,但是当我将它添加到测试时,这会通过 assert @artist.photo.class.name […]

Mongoid不和工厂玩得很好

在我的测试中,Moingoid似乎没有持久地设置嵌入式关系。 在我的用户模型中,我有: def vote_on(账单,价值) 如果my_groups = self.groups my_groups.each做| g | bill.votes.create(:value => value,:user_id => self.id,:group_id => g.id) #结果只有工厂:bill.votes.first.group = nil #和bill.votes.first.user = nil !! #self.id和g.id在测试期间有很好的价值,他们只是不坚持 结束 其他 提出“此用户没有群组”## {self.full_name}“ 结束 结束 其他有用的代码可能是: ##账单模型 class比尔 embeds_many:投票 ##投票模型 投票 包括Mongoid :: Document field:value,:type =>符号#可以是:aye,:nay,:abstain #field:group_type,:type => Integer belongs_to:用户 belongs_to:group embedded_in:bill 结束 ## test 测试“描述性记录应该工作”做 user1 = Factory.build(:user) […]

如何在一步中构建具有子工厂的父级以通过validation

项目必须至少同时创建一个任务以确保validation通过。 这是我用来validation这个的片段: class Project { :minimum => 1 } … end 我所面临的挑战是创建一个合适的工厂,使用FactoryGirl建立一个预先任务的项目。 我正在使用: FactoryGirl.define do factory :task do name “Get this test passing” project end factory :project do title “Complete the application” factory :project_with_tasks do ignore do tasks_count 5 end after(:create) do |project, evaluator| FactoryGirl.create_list(:task, evaluator.tasks_count, project: project) end end end end 现在问题是它失败了,因为它实际上创建了项目,然后尝试创建关联的任务。 该错误报告为: Failure/Error: […]

如何在不同的命名空间下测试控制器以及为什么测试失败?

亲爱的花花公子, 我使用的是Rails 2.3,我创建了一个多态控制器,可以通过属于不同命名空间的Views访问。 这是故事,感谢您提前阅读: 我有这些路线: rake routes | grep appointment new_patient_appointments GET /patients/:patient_id/appointments/new(.:format) {:controller=>”appointments”, :action=>”new”} edit_patient_appointments GET /patients/:patient_id/appointments/edit(.:format) {:controller=>”appointments”, :action=>”edit”} patient_appointments GET /patients/:patient_id/appointments(.:format) {:controller=>”appointments”, :action=>”show”} PUT /patients/:patient_id/appointments(.:format) {:controller=>”appointments”, :action=>”update”} DELETE /patients/:patient_id/appointments(.:format) {:controller=>”appointments”, :action=>”destroy”} POST /patients/:patient_id/appointments(.:format) {:controller=>”appointments”, :action=>”create”} new_admin_doctor_appointments GET /admin/doctors/:doctor_id/appointments/new(.:format) {:controller=>”admin/appointments”, :action=>”new”} edit_admin_doctor_appointments GET /admin/doctors/:doctor_id/appointments/edit(.:format){:controller=>”admin/appointments”, :action=>”edit”} admin_doctor_appointments GET /admin/doctors/:doctor_id/appointments(.:format) {:controller=>”admin/appointments”, :action=>”show”} PUT /admin/doctors/:doctor_id/appointments(.:format) {:controller=>”admin/appointments”, :action=>”update”} […]

Rspec没有路线匹配

我在rspec中遇到以下错误: 1) LandingController landing#index returns http success Failure/Error: get :index ActionController::RoutingError: No route matches {:controller=>”landing”} # ./spec/controllers/landing_controller_spec.rb:7:in `block (3 levels) in ‘ 这是考验 require ‘spec_helper’ describe LandingController do describe “landing#index” do it “returns http success” do get :index response.should be_success end end end 我以root :to => ‘landing#index’ 。 所有其他测试都在通过,只有这一个失败,有人可以帮助我理解为什么吗? 为了完整性,这是rake routes的输出 root / landing#index auth_google_oauth2_callback […]

如何在Rails 2.3中测试多态控制器?

亲爱的花花公子, 我会尽力说明问题的实质。 提前感谢您的耐心等待。 My Polymorphic Controller( Appointments )由两个Views ,第一个属于Admin::Doctor命名空间,第二个属于Patient命名空间。 routes.rb的相关部分: map.resources :patient do |patients| patients.resources :appointments end map.namespace(:admin) do |admin| admin.resources :doctor do |doctors| doctors.resources :appointments end end 所以,我现在可以得到: patients/:patient_id/appointments admin/doctors/:doctor_id/appointments ……和实际路线: rake routes | grep appointment new_patient_appointments GET /patients/:patient_id/appointments/new(.:format) {:controller=>”appointments”, :action=>”new”} edit_patient_appointments GET /patients/:patient_id/appointments/edit(.:format) {:controller=>”appointments”, :action=>”edit”} patient_appointments GET /patients/:patient_id/appointments(.:format) {:controller=>”appointments”, :action=>”show”} PUT /patients/:patient_id/appointments(.:format) {:controller=>”appointments”, […]

rails3 rake没有运行我的测试

在一个新的R3应用程序中,我似乎无法运行任何测试(有2个,“/”的规范function测试和我创建的另一个unit testing) 看到这个输出: rails_app$ rake test –trace (in /Users/jan/portfolio/rails_app) ** Invoke test (first_time, not_needed) rails_app$ 可能有什么不对? first_time是什么意思,not_needed’? 可能与事实有关,我正在使用Mongoid ……

查询参数和assert_generates / assert_routing – 我缺少什么?

我想我已经介绍了使用查询参数测试路由的排列,但没有一种方法通过。 在我的routes.rb中我有以下内容: resources :items 然后,对于我的function测试,我有: require ‘ruby-debug’ require ‘test_helper’ class ItemsControllerTest < ActionController::TestCase # Failure: test_assert_generates_using_params_and_extras(ItemsControllerTest) [test/functional/items_controller_test.rb:7]: The generated path did not match test “assert_generates using params and extras” do assert_generates ‘/items/1/edit?q=abc’, { :controller => ‘items’, :action => ‘edit’, :id => ‘1’, :q => ‘abc’ }, {}, { :q => ‘abc’ } end # Failure: […]

工厂女孩有很多关系(和受保护的属性)

我有这种关系: class Article < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :article attr_protected :article_id end 控制器内部的默认方案如下: @article = Article.create(:title => “foobar”) @comment = @article.comments.create(:content => “w00t”) 我试过写这些工厂: Factory.define :article do |f| f.title “Hello, world” end Factory.define :comment do |f| f.content “Awesome!” f.association :article end 但我的语法关联不正确。 由于评论的article_id protected属性,这有点棘手。 所以我认为如果我在文章工厂内声明关联应该会更好,但我不知道如何处理。 谢谢你的帮助。

在不加载ActiveRecord的情况下测试Rails模块

我正在研究一个Rails应用程序,我正在使用RSpec进行测试。 经过大量等待测试完成之后,我跟随了Gary Bernhardt的建议以及Paul Annesley的这篇信息性post并设置了一个分层spec_helper,我只加载了我绝对需要的Rails部分,以缩短测试时间,并提取function进入我单独测试的单独模块。 这是有道理的。 问题是我有一个模块(扩展ActiveSupport::Concern ),其中包含基于ActiveRecordfunction的实例和类方法,例如绑定到动态查找器,如find_and_create_by_ 。 到目前为止,我已经能够创建一个包含模块和测试的虚拟类,但现在我想将更多逻辑从我的ActiveRecord模型转移到模块中。 具体的例子可能是回调,validation器和方法委托之类的东西,所有这些都与它我正在访问的API有关。 虽然在我的测试中有两个选择,但我现在卡住了: 存根和/或模拟我在模块中调用的每个ActiveRecord方法,这将使测试保持快速但可能使测试代码非常复杂,或者 在测试中需要activerecord,让我的虚拟类inheritance自ActiveRecord::Base并且只测试模块,因为我会测试任何其他的rails模型,这会慢一些但保持测试代码干净。 后一种选择并不真正吸引我,因为我在模块中隔离代码的全部原因是我想将它与Rails分开。 我不是在寻找黑色或白色的答案,但有人会对这种情况有什么建议或最佳实践指示吗? 首先十分感谢!