Tag: 测试

在Rails下测试Ruby Gems

我正在创建一些可以在Rails控制器和视图中使用的gem。 我希望能够独立测试gem。 但是我无法弄清楚如何在gem中创建Rails实例来测试gem将在Rails应用程序中运行。 我更喜欢使用RSpec,但可能适应Test :: Unit解决方案,或者只使用Test :: Unit。 我也对Rails插件的类似解决方案感兴趣。 (我尝试使用谷歌搜索,但“rails gem testing”向我展示了用于测试的gem,而不是如何测试gem。我很确定我在这个主题上看过一篇或两篇文章。)

RoR:测试使用http令牌身份validation的操作

我正在尝试测试在前置filter中使用http令牌身份validation的控制器。 我的问题是,它使用curl传递令牌是可行的,但在我的测试中它总是失败(我使用的是rspec btw)。 尝试了一个简单的测试,看看是否正在传递令牌,但似乎它没有这样做。 我错过了什么来让测试实际将令牌传递给控制器​​? 这是我之前的filter: def restrict_access authenticate_or_request_with_http_token do |token, options| api_key = ApiKey.find_by_access_token(token) @user = api_key.user unless api_key.nil? @token = token #set just for the sake of testing !api_key.nil? end end 这是我的测试: it “passes the token” do get :new, nil, :authorization => ActionController::HttpAuthentication::Token.encode_credentials(“test_access1”) assigns(:token).should be “test_access1” end

如何显示RSpec测试生成的SQL查询日志?

我正在为rails 3应用程序编写规范。 我想测试数据库事务是否真的有效。 能够看到在由规范驱动的情况下生成我的app的sql查询真的很有帮助。 有没有办法像在rails控制台中一样查看查询? 我正在使用Rails 3.0.9,RSpec 2.6和sqlite(稍后将转移到mysql)

如何使用MongoDB和Mongoid在Rails 3上进行适当的数据库测试(TDD)

如何通过Mongoid on Rails使用MongoDB编写适当的unit testing(以及针对该问题的集成测试)? 我问,因为与使用SQLite3相反,即使在运行测试时,我所做的一切仍然存在。 所以目前我正在编写创建测试,然后手动删除我所做的一切。 但是对于集成测试来说,它变得很烦人甚至很复杂。 我做的样本: before(:each) do @user = User.create!(@attr) end after(:each) do # MongoDB is not a transactional DB, so added objects (create) during tests can’t be rollbacked # checking for the existance of a similar object with exact :name and :email (regex make it case insensitive) cleanup = User.where(:name => […]

使用Devise在Rails中进行function测试

经过3年的拖延,今天是我开始测试我的Rails应用程序的那一天。 我的第一步是修复我的Rails 3 beta4应用程序中的失败测试。 我最近3次失败的测试与devise gem及其authenticate_user有关! 我控制器顶部的before_filter中的方法。 通过帮助我解决这个问题,你可以获得很好的业力,因为它可以让我从现在开始使用TDD方法。 这是让我烦恼的错误: 1) Error: test_should_get_accepted(ModerationControllerTest): NoMethodError: undefined method `authenticate!’ for nil:NilClass /test/functional/moderation_controller_test.rb:10:in `test_should_get_accepted’ Devise只是在这个页面中给出了function测试指针和帮助器: http : //github.com/plataformatec/devise但我只是不知道如何将它应用到应用程序中。 你可以给这个测试noob一些关于如何使用这些助手的详细说明吗?

Rails pages_controller_spec.rb测试不应该失败,但是,错误?

在Mac OS X 10.7上一直关注Michael Hart rails 3.0版的Rails教程 $ rspec spec / ……FF Failures: 1) PagesController GET ‘help’ should be successful Failure/Error: get ‘help’ ActionController::RoutingError: No route matches {:controller=>”pages”, :action=>”help”} # ./spec/controllers/pages_controller_spec.rb:45:in `block (3 levels) in ‘ 2) PagesController GET ‘help’ should have the right title Failure/Error: get ‘help’ ActionController::RoutingError: No route matches {:controller=>”pages”, :action=>”help”} # […]

当试图运行rspec时,我得到“未初始化的常量ActiveModel”

当我运行rspec spec我得到以下内容: /usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails/extensions/active_record/base.rb:26:in`’:ininitialized constant ActiveModel(NameError ) 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails/extensions.rb:1:in`requiren’ 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails/extensions.rb:1:in` 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails.rb:8:in,requirement 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails.rb:8:in` 来自/Users/noahc/Dropbox/perfect_setup/spec/spec_helper.rb:4:in`requirement’ 来自/Users/noahc/Dropbox/perfect_setup/spec/spec_helper.rb:4:in`’ 来自/Users/noahc/Dropbox/perfect_setup/spec/controllers/pages_controller_spec.rb:1:in`requirent’ 来自/Users/noahc/Dropbox/perfect_setup/spec/controllers/pages_controller_spec.rb:1:in` 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in”load’ 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in”load_spec_files中的块’ 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in`map’ 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in,load_spec_files’ 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/command_line.rb:18:in“run” 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:80:in“run_in_process” 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:69:in“run” 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:10:in,clock in autorun’ 我的spec_helper.rb看起来像这样: ENV[“RAILS_ENV”] ||= ‘test’ require ‘spec_helper’ require ‘rspec/rails’ require ‘rspec/autorun’ require ‘spork’ Spork.prefork do ENV[‘RAILS_ENV’] ||= ‘test’ require File.expand_path(‘../../config/environment’, __FILE__) require ‘rspec/rails’ RSpec.configure do |config| config.mock_with :rspec config.fixture_path = “#{Rails.root}/spec/fixtures” config.use_transactional_fixtures […]

如何使用page.find使用rspec测试查找变量

我有一个测试用例,我的大多数其他页面至少有一个只是直文的字段,可以使用以下方法找到: page.find(“tr”, text: “What I filled in”).find(“a.tick”).click 此页面的所有选项都是下拉选项,那么如何找到变量呢? 其余语法如下所示: it “edits person job and redirects to index” do expect(p = FactoryGirl.create(:person)).to be_valid() expect(j = FactoryGirl.create(:job)).to be_valid() visit new_job_path select p.name, from: “person_job_person_id” select p.name, from: “person_job_job_id” click_button “create person job” page.find(“tr”, p).find(“a.tick”).click end 它无法找到要点击的p?

为什么我的Cucumber测试在使用Selenium时失败了?

我正在测试一个带有Cucumber / Capybara组合的Rails 3应用程序。 我也试图使用Selenium来测试一些特定于JavaScript的场景,但遇到了我不明白的奇怪困难。 我在Cucumber / Capybara的经历非常低,我对Selenium的经验是零。 这是场景: Scenario: Browsing events Given many events exist And I am on the events page Then I should see a list of 15 events When I follow the first event Then I should be on the event page And I should see a google map And I […]

将灯具转换为Rails中的Factory Girl

我想将我的灯具迁移到Rails中的“Factory Girl”。 有没有简单的方法来转换factories.rb文件中的所有yml文件?