Tag: unit testing

如何在Ruby on Rails和Test :: Unit上测试Controllerfilter

我们在Ruby on Rails中有一个包含许多filter的大型应用程序。 其中一些filter可能很复杂。 我正在寻找一种通过unit testing单独测试这些滤波器的方法。 现在我通过一个使用function测试的动作测试它们来测试它们。 这只是感觉不对劲。 有没有人有这方面的建议或经验?

在Rails中,为什么我的邮件主体只在我的测试中空了?

我有一个actionmailer类和相关的开销,它完美地工作。 但是,在我的unit testing(rails default minitest)中,电子邮件正文是空的。 这是为什么? 我的邮件课: class TermsMailer < ApplicationMailer default from: "info@domain.com" def notice_email(user,filename) @user = user @file = filename mail(to: "info@domain.com", subject: 'Data downloaded') end end 我的测试: require ‘test_helper’ class TermsMailerTest < ActionMailer::TestCase test "notice" do email = TermsMailer.notice_email(users(:me),'file.ext').deliver_now assert_not ActionMailer::Base.deliveries.empty? assert_equal ['info@domain.com'], email.from assert_equal ['info@domain.com'], email.to assert_equal 'Data downloaded', email.subject assert_equal […]

具有完整堆栈跟踪的unit testing

这似乎是一个愚蠢的问题,但我无法在任何地方找到答案。 我正在使用Rails 3.1和Test :: Unit。 当测试失败时,我只看到堆栈的顶部,例如: ERROR creating a message sends emails (1.35s) NoMethodError: undefined method `project’ for nil:NilClass /Users/ghempton/.rvm/gems/ruby-1.9.2-p180@gt/gems/activesupport-3.1.3/lib/active_support/whiny_nil.rb:48:in `method_missing’ 如何查看完整堆栈跟踪?

相关工厂的工厂女孩​​唯一性validation失败

我有(简化)工厂定义如下: factory :league do acronym ‘NBA’ end factory :division do league end 分部属于联盟。 当我定义这个工厂时,我的假设就是创建了一个联盟,并且这个联盟会被一遍又一遍地重复使用以给分区一个真正的联盟。 相反,我在FactoryGirl.create(:division)的第二次调用中遇到错误,因为联盟的acronym应该是唯一的。 class League < ActiveRecord::Base validates :acronym, uniqueness: true end 在测试中导致以下中断 ActiveRecord :: RecordInvalid:validation失败:已经删除了缩略词 我该如何解决这个问题,最好不要在测试设置中创建层次结构? 如果我想要实现的目标比factory_girl更好,请建议

Rails:旧的,不存在的数据库表导致测试错误

我一直试图在我最近负责的Rails应用程序中运行一个“真实”unit testing,mac_test.rb。 它看起来像这样: require ‘test_helper’ class MacTest < ActiveSupport::TestCase # Replace this with your real tests. test "the truth" do assert true end end 我运行rake db:test:prepare我正在尝试使用ruby -I test test/unit/mac_test.rb运行测试。 但是,我得到以下缩写错误输出: Loaded suite test/unit/mac_test Started EEEEEEEEEEEEEEE Finished in 0.377261 seconds. 1) Error: test_datas(ActionController::IntegrationTest): ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: vendors: DELETE FROM “vendors” WHERE 1=1 2) Error: […]

如何从Rails集成测试中访问会话?

考虑以下集成测试: test “if there is no user in session, redirect to index and flash message” do open_session do |sess| post ‘/login’, :email => users(:client).email, :password => ‘rumpelstiltskin’ sess[:user] = nil get ‘/user_page’ assert_redirected_to index_path assert_equal “Your session has expired. Please log in again”, flash[:notice] end end 这会产生错误: undefined method ‘[]=’ 并且,更改为sess.session[:user] = nil也会生成错误: NoMethodError: undefined […]

是否应在rails中测试本机validation?

大家都知道自动化测试是件好事。 不是每个人都知道要测试什么。 我的问题是,是否应在应用程序中测试validate_presence_of,validate_uniqueness_of等本机validation。 在我的办公室,我们三个人,一个人认为应该进行测试,一个人认为不应该这样,我在空中。

不需要的符号来转换哈希键的字符串

当我在我的控制器中分配时 @my_hash = { :my_key => :my_value } 并通过执行测试该控制器 get ‘index’ assigns(:my_hash).should == { :my_key => :my_value } 然后我收到以下错误消息: expected: {:my_key=>:my_value}, got: {“my_key”=>:my_value} (using ==) 为什么这种自动符号转换为字符串? 为什么它会影响哈希的键?

轨道unit testing是否应该打到数据库?

我一直在为我的rails应用程序编写测试。 我使用TestUnit进行unit testing和function测试。 我也使用黄瓜进行GUI测试。 但我发现http://www.dcmanges.com/blog/rails-unit-record-test-without-the-database说unit testing最好不要打到数据库。 我同意击中数据库需要相当长的时间。 我已经使用spork来减少环境负荷。 在测试rails应用程序时,最佳做法是什么?

RubyMineunit testing – 测试框架意外退出

当我尝试从RubyMine中运行测试时,我遇到了问题。 但奇怪的是,当我从命令行运行测试时,它工作正常。 “测试框架意外退出” /usr/local/rvm/rubies/ruby-1.9.3-p392/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) -Itest /Users/sabour/Desktop/EIP/project/test/controllers/categories_controller_test.rb Testing started at 1:39 AM … Run options: –seed 14336 # Running tests: /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228: warning: nested repeat operator + and ? was replaced with ‘*’ … Finished tests in 2.554592s, 1.1744 tests/s, 8.6119 assertions/s. 3 tests, 22 assertions, 0 failures, 0 errors, 0 skips Process finished with […]