Tag: 测试

如何测试使用机械化的ruby应用程序

我写了一个小程序,它使用Mechanize来遍历一个站点。 我想为它编写测试,但不希望每次运行测试时它都会实际登录到站点。 我想嘲笑互联网,所以当它进入某个网站时,它只是返回存储的结果。 这是一个小例子,假装我的代码的目的是从谷歌主页上拉链接,所以我写了一个测试,以确保我的代码找到的第一个链接有文本“图像”。 我可能写这样的东西: require ‘rubygems’ require ‘mechanize’ require ‘test/unit’ def my_code_to_find_links google = WWW::Mechanize.new.get(‘http://www.google.com’) # … # some code to figure out which liks it wants # … google.links end class TestGoogle < Test::Unit::TestCase def test_first_link_is_images assert_equal 'Images' , my_code_to_find_links.first.text end end 如何模拟google.com以便我可以测试my_code_to_find_links而无需实际访问互联网的所有开销? 谢谢 – 乔什

如何在Rails测试中指定POST参数?

使用Test :: Unit和Shoulda。 试图测试Users.create 。 我的理解是Rails表单为这样的对象发送params: user[email] 在你的行动中变成哈希,对吧? params[:user][:email] 好的,所以在我的测试中我试过…… setup { post :create, :post => { ‘user[email]’ => ‘invalid@abc’ } } 和 setup { post :create, :post => { :user => { :email => ‘abc@abcd’ } } } 在这两种情况下,在我的行动中, params[:user]为零。

测试佣金任务

我正在开发一个依赖于许多自定义Rake任务的ROR应用程序。 测试它们的最佳方法是什么?

未定义的局部变量或方法 – 使用Beaker测试Puppet模块

我对这一切都很陌生。 我正在尝试使用Beaker测试一个木偶模块。 我一直这样: NoMethodError: undefined method `describe’ for #Beaker::TestCase:0x007fd6f95e6460 /Users/user1/beaker/Puppet/puppet-files/spec/classes/unit_spec.rb:3 /Users/user1/.rvm/gems/ruby-2.2.7/gems/beaker-3.24.0/bin/beaker:9 /Users/user1/.rvm/gems/ruby-2.2.7/bin/ruby_executable_hooks:15 /Users/user1/.rvm/gems/ruby-2.2.7/bin/ruby_executable_hooks:15. This is the command that I’m running – “beaker –hosts myhost.yaml –pre-suite spec”. 我的unit_spec.rb包含: require ‘puppetlabs_spec_helper/rake_tasks’ describe ‘application’ do context ‘applied to supported operating system’ do on_supported_os.each do |os, facts| context “#{os}” do let(:facts) do facts end context “without any parameters” do […]

使用RSpec测试警告

是否有可能使用RSpec测试Ruby的警告? 像这样: class MyClass def initialize warn “Something is wrong” end end it “should warn” do MyClass.new.should warn(“Something is wrong”) end

Rails在哪里存储通过在测试期间保存activerecord对象而创建的数据?

Rails在哪里存储通过在测试期间保存activerecord对象而创建的数据? 我以为我知道这个问题的答案:显然在_test数据库中 。 但看起来这不是真的 ! 我使用这个系统来测试在rspec测试期间保存的ActiveRecord数据发生了什么: $ rails -d mysql test $ cd测试 $ nano config / database.yml … …创建mysql数据库test_test,test_development,test_production $ script / generate rspec $ script / generate rspec_model foo 编辑Foo迁移: class CreateFoos $ rake db:migrate 编辑spec / models / foo_spec.rb: 需要File.expand_path(File.dirname(__ FILE__)+’/../spec_helper’) 描述Foo做 之前(:每个)做 @valid_attributes = { :bar => 12345 } 结束 它“应该创建一个给定有效属性的新实例” […]

测试ssh连接

我的项目的一个重要部分是使用ssh登录到远程服务器并对其执行某些操作: Net::SSH.start(@host, @username, :password => @password) do |ssh| ssh.exec!(rename_files_on_remote_server) end 怎么测试呢? 我想我可以使用本地ssh服务器并检查其上的文件名(也许它可能在我的test / spec目录中)。 或许有人可以指出我更好的解决方案?

为什么我的RSpec测试失败,但我的应用程序正在运行?

我刚刚完成了Ruby on Rails教程的第10章 ,添加了编辑/更新,索引和销毁用户的function。 一切似乎都在我的应用程序中正常工作,但是当我运行RSpec时,我的许多测试都失败了。 我设置的users_controller_spec与书的设置完全一样,我的应用程序代码也是一样的。 一个问题可能是我使用Rails 3.1.1而不是他在书中使用的Rails 3.0? 它对于以前的测试来说并不是一个问题,只是偶尔会出现几行不同的代码。 在我开始第10.2.1节后 ,问题开始出现。 以下是我看到的错误列表,如果您需要更多信息,请告诉我。 谢谢! 1) UsersController GET ‘index’ for signed-in users should be successful Failure/Error: response.should be_success expected success? to return true, got false # ./spec/controllers/users_controller_spec.rb:31:in `block (4 levels) in ‘ 2) UsersController GET ‘index’ for signed-in users should have the right title Failure/Error: response.should […]

如何使用Ruby minitest框架调用某些方法?

我想测试一个函数是否使用minitest Ruby正确调用其他函数,但我找不到一个适当的assert来测试doc 。 源代码 class SomeClass def invoke_function(name) name == “right” ? right () : wrong () end def right #… end def wrong #… end end 测试代码: describe SomeClass do it “should invoke right function” do # assert right() is called end it “should invoke other function” do # assert wrong() is called end […]

如何用Ruby学习TDD?

我一直在使用ruby大约一个月,我真的很喜欢它。 但是,我使用甚至学习TDD都非常困难。 我的大脑不会那样运作…… 我真的,真的想学习TDD,但说实话我有点困惑。 我在Google上找到的所有文章都主要针对Rails,这对我来说并不感兴趣,因为我想学习如何对任何ruby应用程序进行高效测试,从简单的单文件脚本到复杂的gem,而不是网络应用。 此外,还有很多框架和很少的入门教程。 有人可以给我任何关于如何学习TDD的建议,这样我至少可以开始认为自己是一个有抱负的rubyist吗?