Tag: unit testing

在minitest中使用帮助器

我正在尝试使用minitest测试我的app类,但是我收到一条错误,指出undefined method image_path 。 我怎么能绕过这个? app_test.rb require ‘test_helper’ class AppTest < ActiveSupport::TestCase setup do @app = apps(:app_one) end test 'should have icon_url' do assert(@app.icon_url == image_path('icn-medium-norm.png')) end end 应用程序/模型/ app.rb class App (time) { where(‘created_at > ?’, time) } scope :ios, -> { where(“app_type = ‘ios’ “) } scope :android, -> { where(“app_type = ‘android’ […]

对于简单路由,RSpec控制器测试失败

这可能是一个非常基本的错误,但我仍在学习。 =) 我的routes.rb只包括 WebPortal::Application.routes.draw do resources :categories end 如果我理解正确,这应该(以及其他)地图/categories到CategoriesController.index 。 这个控制器看起来像 class CategoriesController < ApplicationController def index end end 相应的视图文件存在,rails服务器可以很好地提供此页面。 但我的RSpec测试 describe CategoriesController do describe “GET :index” do it “should be succesful” do get :index response.should be_succes end end end 失败了 Failure/Error: get :index ActionController::RoutingError: No route matches {:controller=>”categories”} 我在这做错了什么? 编辑: 命令rake routes给出 rake routes […]

current_user使用Devise在控制器中返回TrueClass而不是User

我在Rails 2.3.8项目上使用Devise 1.0.8。 在我的控制器测试中,我有: context “when current_user is admin” do should “render” do sign_in Factory(:admin) get :index assert_response :success end end 在控制器本身,有以下before_filter: def redirect_if_not_admin puts current_user redirect_to root_path unless current_user.try(:admin?) end 这是我测试的输出: true false NoMethodError: undefined method `admin?’ for true:TrueClass 因此,当我在测试中使用sign_in时,current_user为true。 但是当我不这样做时,current_user是nil,所以肯定会受到影响。 在此先感谢您的帮助。

测试rake任务中定义的方法

我想测试rake任务中定义的方法。 耙文件 #lib/tasks/simple_task.rake namespace :xyz do task :simple_task => :environment do begin if task_needs_to_run? puts “Lets run this…” #some code which I don’t wish to test … end end end def task_needs_to_run? # code that needs testing return 2 > 1 end end 现在,我想测试这个方法, task_needs_to_run? 在测试文件中我该怎么做? 补充说明:我理想的是也希望在rake任务中测试另一个私有方法……但我可以稍后再担心。

使用Rails进行应用程序测试

这更像是一般性问题和某种最佳实践讨论。 如何测试Rails应用程序? 有像BDD和TDD这样的咒语和像RSpec和Cucumber这样的框架但是多少就足够了,最好的方法是什么? 使用Cucumber作为集成测试是否足够? 你们也在编写额外的unit testing吗? 那么你的Rails测试策略是什么以及如何? 期待好的意见……

exception消息:在rails应用程序中没有要加载此类文件

我在rails应用程序中包含了test_helper.rb 。 我甚至在test->unit下的test.rb文件中包含了require ‘test_helper’ 。 当我在RubyMine中为应用程序运行所有测试时,我收到以下错误: 我有所有RubyMine 3.2,ruby 1.9.2p290和Rails 3.1.0的最新版本。 /Users/im/.rvm/rubies/ruby-1.9.2-p290/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /Applications/RubyMine 3.2.4.app/rb/testing/runner/tunit_in_folder_runner.rb Testing started at 3:19 PM … Work directory: /Users/im/RubymineProjects/BackChannelApp} Loading files…. ========================================= Fail to load: /Users/im/RubymineProjects/BackChannelApp/test/performance/browsing_test.rb:1 Exception message: no such file to load — test_helper /Users/im/RubymineProjects/BackChannelApp/test/performance/browsing_test.rb:1:in `require’ /Users/im/RubymineProjects/BackChannelApp/test/performance/browsing_test.rb:1:in `’ /Applications/RubyMine 3.2.4.app/rb/testing/runner/tunit_in_folder_runner.rb:48:in `require’ /Applications/RubyMine 3.2.4.app/rb/testing/runner/tunit_in_folder_runner.rb:48:in `block in load_scripts_to_object_space’ /Applications/RubyMine 3.2.4.app/rb/testing/runner/tunit_in_folder_runner.rb:40:in `each’ […]

Rails 2.3.2unit testing在使用普通ruby运行时通过,在使用rake测试运行时失败:单位

在unit testing中创建记录时,当使用rake test:units运行时,我得到一个’ActiveRecord :: RecordInvalid’。 当我使用ruby(’ruby unit / blah_test.rb’)手动运行unit testing时,我没有收到此错误。 我已经将问题缩小到我的模型中存在的validation。 它是一个包含validation,它实际上是针对不同的模型执行查找以获取有效值。 我认为这与没有及时加载的灯具有关,但我的test_helper.rb中确实有’灯具:全部’(我也试过在unit testing中加入’灯具:全部’。 有没有人对如何进一步缩小范围有任何建议? 谢谢。

使用Rails 3.1进行测试时,“ActiveSupport :: TestCase:Class”的未定义方法`fixtures’

我正在为Gem编写测试,经过几个小时试图摆脱这种情况后,决定环顾四周 – 令我惊讶的是,谷歌上没有一个参考它(除了一个关于mongoid,人们只是忽略它)。 所以,问题很简单:我在测试初始化​​时有这个块: class ActiveSupport::TestCase fixtures :all end 但是测试无法执行: `’: undefined method `fixtures’ for ActiveSupport::TestCase:Class (NoMethodError) gem依赖于Rails 3,每个依赖项都经过检查和双重检查。 代码在github上,万一有人想检查( https://github.com/herval/acts_as_recommendable )我没有想法。 有人共用灯吗?

Rails中的Authlogic和单元/function测试问题

我正在学习如何在Rails中完成unit testing,并且我遇到了涉及Authlogic的问题。 根据文档 ,在测试中使用Authlogic的东西需要一些东西: test_helper.rb中: require “authlogic/test_case” class ActiveSupport::TestCase setup :activate_authlogic end 然后在我的function测试中我可以登录用户: UserSession.create(users(:tester)) 问题似乎源于test_helper.rb中的setup :activate_authlogic行,只要包含它,运行function测试时会出现以下错误: NoMethodError: undefined method `request=’ for nil:NilClass authlogic (2.1.3) lib/authlogic/controller_adapters/abstract_adapter.rb:63:in `send’ authlogic (2.1.3) lib/authlogic/controller_adapters/abstract_adapter.rb:63:in `method_missing’ 如果我删除setup :activate_authlogic并将Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)到test_helper.rb,我的function测试似乎工作但现在我的unit testing失败: NoMethodError: undefined method `params’ for ActiveSupport::TestCase:Class authlogic (2.1.3) lib/authlogic/controller_adapters/abstract_adapter.rb:30:in `params’ authlogic (2.1.3) lib/authlogic/session/params.rb:96:in `params_credentials’ authlogic (2.1.3) lib/authlogic/session/params.rb:72:in `params_enabled?’ authlogic (2.1.3) […]

在Rails 5控制器集成测试中设置cookie.signed

想象一下,有一个控制器集成测试调用控制器方法的场景,其中cookie.signed用于某些完整性检查。 调节器 # app/controllers/foo_controller.rb def index entity = FooEntity.find_by_id(params[:id]) if entity.nil? raise ActionController::BadRequest, ‘Could not find requested entity.’ else @is_authorized = entity.token == cookies.signed[:token] if @is_authorized # Success! The path to be tested. else raise ActionController::BadRequest, ‘Unauthorized cookie token.’ end end end 控制器测试 # app/test/controllers/foo_test.rb require ‘test_helper’ class FooControllerTest < ActionDispatch::IntegrationTest test 'should be working' […]