Tag: rspec

什么测试框架用于Rails?

什么unit testing框架用于Rails? 我正在阅读这本书(Pragmatic Programmers Agile Dev.with Rails),它展示了如何在Rails中进行unit testing。 这本书向我展示了默认的rails测试套件( Test::Unit的子类)。 这是Rails社区中使用的主要测试框架吗? 我在做普通ruby时使用RSpec,我也希望能够在Rails中使用它(如果不是太麻烦)?

如何使用rspec存根/模拟对命令行的调用?

我正在尝试从命令行工具测试输出。 如何使用rspec“伪造”命令行调用? 执行以下操作不起作用: it “should call the command line and return ‘text'” do @p = Pig.new @p.should_receive(:run).with(‘my_command_line_tool_call’).and_return(‘result text’) end 如何创建该存根?

让nokogiri使用更新版本的libxml2

我一直试图让Nokogiri安装在我的电脑(Mountain Lion)上与rspec和capybara一起使用,但对于我的生活,我无法让它运行正常。 据我所知,问题在于nokogiri使用了错误版本的libxml2。 到目前为止,我尝试使用Homebrew卸载并重新安装libxml2(确保它是最新版本),使用bundle卸载并重新安装nokogiri,并指定安装nokogiri gem时Homebrew安装的libxml2文件的确切路径。 我最近的安装说明看起来像这样 sudo gem install nokogiri — –with-xml2-include=/usr/local/Cellar/libxml2/2.9.0/include/libxml2 –with-xml2-lib=/usr/local/Cellar/libxml2/2.9.0/lib –with-xslt-dir=/usr/local/Cellar/libxslt/1.1.28 其中所有这些位置都正确对应于安装工具的位置。 但是,在运行bundle exec rspec spec/requests/static_pages.rb ,我仍然得到这个输出: /Users/alex/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.9/lib/nokogiri.rb:28:in `require’: dlopen(/Users/alex/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.9/lib/nokogiri/nokogiri.bundle, 9): Library not loaded: /usr/local/lib/libxml2.2.dylib (LoadError) Referenced from: /Users/alex/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.9/lib/nokogiri/nokogiri.bundle Reason: Incompatible library version: nokogiri.bundle requires version 12.0.0 or later, but libxml2.2.dylib provides version 10.0.0 – /Users/alex/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.9/lib/nokogiri/nokogiri.bundle from /Users/alex/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.9/lib/nokogiri.rb:28:in `’ from /Users/alex/.rvm/gems/ruby-1.9.3-p286@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:72:in `require’ […]

在rspec中模拟方法链

有一系列方法可以获取user对象。 我试图嘲笑以下内容以返回我的Factory Girl的user @current_user = AuthorizeApiRequest.call(request.headers).result 我可以模拟对象,直到call方法,但我坚持嘲笑result方法 allow(AuthorizeApiRequest).to receive(:call).and_return(:user)

Rspec测试CSV文件下载

我想确保我的CSV下载包含正确的列。 当我使用RSpec测试CSV下载时,我无法访问文件内容。 如何访问CSV文件的内容? require ‘spec_helper’ include Devise::TestHelpers describe Admin::ApplicationsController do before(:each) do @application = FactoryGirl.create :application @user = FactoryGirl.create( :admin_user ) sign_in @user end it “downloads a csv” it “gives us only the columns we want” do get :index, format: :csv p response.body p response.headers end end 测试的输出: # This is the output in the […]

如何使用rspec / rspec-mocks来存根类方法

我正在使用rspec-mock进行测试驱动开发。 我开始实现一个单独的类,并使用rspec-mock来模拟/存根其他类。 模拟尚未实现的类的对象效果很好。 但是,当我尝试模拟一个尚不存在的类的类方法时,我还没有成功。 我的类“Hashes”应该有一个类方法“calculate_hashes”接收文件名并返回一个哈希值。 我试过了 allow(Hashes).to receive(:calculate_hash) do |file| # looks up what to return end 给出错误“哈希不是一个类”。 然后我实施了一个类“哈希” class Hashes end 然后只尝试以相同的方式存根类方法。 这给出了错误“Hashes没有实现:calculate_hash”当我然后将方法添加到我的类定义时: class Hashes def self.calculate_hash(filename) end end 它最终工作,我的这个类方法的存根使用“allow(Hashes)”工作,如上例所示。 我只是想知道是否有一种方法可以在不编写此类骨架的情况下完成此操作。 或者我可能试图以不恰当的方式完成某些事情? 或者rspec-mock可能不是正确的工具呢? 任何帮助是极大的赞赏。

Counter_cache列的Rspec测试返回0

几天来我一直试图找到应该很容易做到的接缝的底部…然而我仍然对铁轨和ruby的世界很新,我只是不能解决这个问题。 ..:p 无论如何,我遇到的问题是我的模型中有一些:counter_cache列,这些列在手动测试时都非常好用。 但是,我想做TDD的事情,我不能缝在rspec测试他们的一些未知的原因? 无论如何这里是我的模型的一个例子(用户,评论和媒体): class User < ActiveRecord::Base has_many :comments has_many :media, dependent: :destroy end class Comment true belongs_to :user, :counter_cache => true validates :user_id, :presence => true validates :content, :presence => true, :length => { :maximum => 255 } end class Medium true has_many :comments, as: :commentable validates :user_id, presence: true validates :caption, […]

Rspec测试页面内容中的html实体

我正在编写请求规范,并希望测试字符串“Reports»Aging Reports”的存在。 我得到一个错误(无效的多字节字符),如果我直接在我的匹配表达式中输入字符,所以我尝试了这个: page.should have_content(“Reports » Aging Reports”) 这使测试失败并显示以下消息: expected there to be content “Reports » Aging Reports” in “\n Reports » Aging Reports\n 我尝试过像.html_safe这样的东西没有成功。 有没有办法测试包含html实体的文本? 编辑: 这是html源代码的相关区域: Reports » Aging Reports

Rspec包在TextMate和rvm中被破坏

自从我开始使用rvm以来,我度过了一段艰难的时光。 我已经完成了所有的rvm / textmate设置并拥有最新的捆绑包但我仍然无法从textmate运行Rspec测试 。 我有来自github.com/rspec/rspec-tmbundle.git的最新软件包,它安装在~/Library/Application\ Support/TextMate/Bundles/ RSpec.tmbundle RVM默认使用系统ruby 1.8.6 Rspecgem版本 gem列表 – 本地| grep规范blue_light_special(0.2.0)rspec(2.2.0)rspec-core(2.2.1,2.0.1)rspec-expectations(2.2.0,2.0.1)rspec-mocks(2.2.0,2.0.1) rspec-rails(2.0.1,1.3.2) TextMate的 TM_RUBY = /用户/ jspooner / .rvm /斌/ RVM-自动ruby 错误: rspec / core(LoadError) / Users / jspooner / Library / Application Support / TextMate / Bundles / RSpec.tmbundle / Support / lib / rspec / mate.rb:29:in require’: no such […]

使用Capybara下载CSV

对于rspec测试,我需要以CSV文件格式下载报告并validation给出的信息。 单击按钮时,将从网页生成报告。 浏览器保存对话框已打开,提供打开或保存选项。 如何使用rspec和Capybara将文件保存到计算机中?