Tag: unit testing

Ruby“test / unit”,如何在断言中显示消息

我在ruby脚本中有断言。 这些断言中的每一个也都有描述断言的消息。 所以我有两个围绕这个问题的问题 我希望在测试过程中显示与每个断言关联的消息。 目前,只有在任何事情失败时才显示消息 当任何断言失败时,整个测试退出。 即使一个断言失败,我如何继续测试? 我正在使用的示例代码 assert_match(“hey”, “hey this is a test”, “The word exists in the string”) 所以输出目前看起来像 – ruby ruby11.rb -vv Loaded suite ruby Started test_ruby(Ruby): F Finished in 40.555587 seconds. 1) Failure: test_ruby11(Ruby11) [ruby11.rb:73]: Text box not displayed is not true. 1 tests, 3 assertions, 1 failures, 0 errors 我希望它看起来像 […]

使用minitest进行多次测试

我有一个应用程序,其中一些规格写入minitest 。 像往常一样,我用rake开始。 因为有时我得到一个随机的结果,我的规格可以通过一次,而另一次失败。 在这种情况下,我可以保留序列号,并在修复后重播。 因为我有这种测试(随机结果),我通常运行rake很多次,只是为了确保应用程序没问题。 我想知道是否有一种很好的方法来执行多次rake测试(例如100次),如果出现任何故障或任何错误,请停止它们?

使用子项存在validationFactory Girl创建父项和子项

有一个包含许多旅行的发票的项目。 新的故事以我的方式要求发票必须旅行。 我添加了一个validationvalidates :trips, presence: true trip validates :trips, presence: true但是现在它已经炸毁了我的一些测试,因为FactoryGirl在创建相关行程之前试图保存发票。 FactoryGirl.define do factory :invoice do sequence(:invoice_id) { SecureRandom.uuid} merchant amount 100.00 item_count 1 paid false currency “GBP” invoice_type “pre-flight” service_rendered false cancelled false after(:create) { |object| create(:trip, invoice_id: object.invoice_id)} end end 我该怎么做才能创建这些对象。 最好是在工厂级别,因为有许多测试利用这种行为(并且目前由于它而失败)。 这似乎是测试级别的一个很好的解决方案。 更新仍然努力让我的测试现在变绿。 42使用以下代码进行测试时出错。 validation失败:旅行不能为空 我在FactoryGirl代码中的当前更新行 before(:create) { |object| object << build(:trip, […]

我可以测试Sinatra post方法是否成功保存到YAML商店?

我无法在任何地方找到关于如何使用Rack :: Test测试Ruby / Sinatra post方法成功将数据保存到YAML存储/文件的基本解释。 ( 这解释了测试get ,我可以做(!),但没有post ;其他提到测试使用rack / test的post方法似乎无关紧要。)对于自学,我正在构建一个“待办事项”应用程序在Ruby / Sinatra和我正在尝试使用TDD的一切,并像一个好小男孩一样进行unit testing。 我的要求是:当用户发布新任务时,它将保存在YML存储中。 我正在考虑通过查看是否在对用户的响应中显示“任务已保存”来测试这一点(当然这不是直接测试事物本身……但我还想测试它): assert last_response.body.include?(“Task saved”) 或以某种方式测试测试任务的描述现在是否在YML文件中。 我想我可以打开YML文件并查看,然后从YML文件中删除它,但我很确定这不是我应该做的。 我已确认post确实正确保存到YML文件: get(‘/’) do |*user_message| # prepare erb messages @user_message = session[:message] if session[:message] @overlong_description = session[:overlong_description] if session[:overlong_description] session[:message] = nil # clear message after being used session[:overlong_description] = nil # ditto @tasks […]

Selenium:在Selenium RC ruby​​驱动程序中等待_for_ *和朋友

对于Selenium RC的ruby驱动程序,有没有任何实现所有漂亮的Selenium on Rails方法,如wait_for_visible , assert_not_text_present ,…? 如果没有,我将如何实现wait_for_visible之类的东西?

Stub(…)收到意外消息(…)(没有args)

我尝试使用RR编写测试。 我需要的是模型对象的存根。 describe ApplicationController do subject(:application_controller) { ApplicationController.new } let(:messages) { [‘a1’, ‘a2’, ‘a3’ ] } let(:model) { Object.new } it ‘should copy errors to flash’ do stub(model).error_messages { messages } flash[:error] == nil subject.copy_errors_to_flash(model) flash[:error].should == messages end end 我得到的是 ApplicationController should copy errors to flash Failure/Error: stub(model).error_messages { messages } Stub # received […]

Ruby on Rails测试“预期响应但返回200

我是一位经验丰富的Web开发人员,我开始在rails上学习ruby,并使用HTML和CSS绑定到rails的代码中。 我有一个测试网站,我试图在ruby上运行它。 该测试将在网络上的一个轨道应用程序下显示一个包含主页,帮助,关于和联系页面的网站。 require ‘test_helper’ class StaticPagesControllerTest < ActionController::TestCase test "should get home" do get :home assert_response :success assert_select "title", "Ruby on Rails Tutorial Sample App" end test "should get help" do get :help assert_response :success assert_select "title", "Help | Ruby on Rails Tutorial Sample App" end test "should get about" do get :about assert_response […]

如何在rspec示例中存根全局日志记录function

我正在使用一些记录到全局静态日志记录类的代码,例如: GlobalLog.debug(“Some message”) 但是在我的测试中,我不想包含真正的日志,因为它引入了许多不需要的依赖项。 所以我想嘲笑它: describe “some function” do before(:all) do log = double(‘log’) GlobalLog = log log.stub(:debug) end … end 不幸的是,因为在每个示例之后清除了双精度数,所以不允许这样做: https://www.relishapp.com/rspec/rspec-mocks/docs/scope 如果我将before(:all)更改为before(:each) ,代码可以正常工作,但是我收到警告: warning: already initialized constant GlobalLog 这会堵塞我的测试输出,所以我想避免警告。 有清洁的解决方案吗?

如何用Rails测试ActiveRecord?

我有一个项目,我使用ActiveRecord将信息存储在sqlite db文件中。 我没有使用Rails,AR似乎完美地完成了这项工作。 我的问题是如何测试我的类没有击中数据库? 我找到了一些可以解决这个问题的gem(FactoryGirl,UnitRecord),但是它们可以用于Rails。 class News < ActiveRecord::Base belongs_to :feed def delete_old_news_if_necessary # time_limit = Settings::time_limit return if time_limit.zero? News.destroy_all("date feed_id) end def news_for_feed(feed_id) News.find(feed_id) end end 我读到我可以做一个列存根: Column = ActiveRecord::ConnectionAdapters::Column News.stubs(:columns).returns([Column.new(),…]) 这是进行这些测试的正确方法吗? 另外,什么时候最好只有一个单独的数据库用于测试和创建它,运行测试,并删除它?

在RSpec中捕获STDOUT

我是Ruby的unit testing的新手,我在创建一个检查STDOUT的规范时遇到了一些麻烦。 我知道如果我正在测试的代码是在类/方法中,如何测试STDOUT,但我想测试一个来自常规Ruby文件的puts语句,该文件不属于Class或Method。 这是来自my_file.rb的简单一行 puts “Welcome to the Book Club!” 这是规格 my_spec.rb require_relative ‘my_file.rb’ describe “Something I’m Testing” do it ‘should print Welcome to the Book Club!’ do expect {$stdout}.to output(“Welcome to the Book Club!\n”).to_stdout end end 我收到以下错误消息: Failures: 1) Something I’m Testing should print Welcome to the Book Club! Failure/Error: expect {$stdout}.to output(“Welcome to […]