Tag: rspec

机架测试失败:JSON请求尚未响应

我正在尝试按照Yehuda Katz的书“ Rails 3 in Action”第13章中提供的Ticketee示例为我的Ruby项目创建一个JSON API。这是一个适用于我的环境的第353页描述的RSpec测试。 # /spec/api/v1/farms_spec.rb # Reduced to the minimum code. require “spec_helper” include ApiHelper # not sure if I need this describe “/api/v1/farms”, type: :api do context “farms viewable by this user” do let(:url) { “/api/v1/farms” } it “json” do get “#{url}.json” assert last_response.ok? end end end 当我运行测试时,我得到以下输出… $ rspec […]

如何从ruby中的反引号命令中获取颜色?

在ruby文件中: 当我做system(“rspec file_spec.rb”)我得到一个很好的彩色输出 。 我这样做的时候: result = `rspec file_spec.rb` puts result 我根本没有颜色。 有没有办法保留颜色 ? 顺便说一句,如果重要的话,我在OSX上使用Terminal.app。

无法测试应该是belongs_to,在Rails上缺少id外键

您好我一直在寻找一种测试模型关系的方法,并偶然发现应该是gem shoulda(3.5.0) 应该上下文(1.2.1) 应该匹配(2.8.0) 不幸的是,我尝试用rspec测试一个简单的例子 describe Region do it “should have a city” do should belong_to(:city) end end 我总是得到一个消息 Region should have a city Failure/Error: should belong_to(:city) Expected Region to have a belongs_to association called city (Region does not have a city_id foreign key.) # ./spec/models/region_spec.rb:5:in `block (2 levels) in ‘ 虽然我的关系有问题,但我已经测试了在rails console成功创建了一个与城市绑定的区域。 我肯定错过了什么!! 编辑模型和迁移 […]

Ruby(猴子修补arrays)

返回获取更多关于Bloc课程的帮助。 决定带给你们一些我正在使用Monkey Patching the Array Class的问题。 这个任务有8个规格可以满足,我现在卡在一个左边,我不知道该怎么办。 我只会给你RSpecs和我遇到麻烦的部分的书面要求,因为其他一切似乎都在传递。 此外,我将包括他们开始给我的入门脚手架,因此代码中没有混淆或无用的添加。 以下是Array Class Monkey Patch的书面要求: 编写一个在Array类的实例上调用的新new_map方法。 它应该使用它所调用的数组作为隐式( self )参数,但在其他方面表现相同。 ( 未完成 ) 写一个new_select! 行为与select类似的方法,但会改变调用它的数组。 它可以使用Ruby的内置集合选择方法。 ( 完整 ) 以下是有关Array类需要满足的RSpec: 注意:“返回具有更新值的数组”是唯一未传递的规范。 describe Array do describe ‘#new_map’ do it “returns an array with updated values” do array = [1,2,3,4] expect( array.new_map(&:to_s) ).to eq( %w{1 2 3 4} ) expect( […]

使用rspec检查错误的枚举

有谁知道如何在rspec中检查无效的枚举响应? 我可以检查以确保枚举不是没有错误,但是当我检查确保坏的枚举值不起作用时,我收到错误。 这些是两个规格: it ‘is not valid without question type’ do expect(build(:question, question_type: nil)).to have(1).errors_on(:question_type) end it ‘is not valid with a bad question type’ do expect(build(:question, question_type: :telepathy)).to have(1).errors_on(:question_type) end 这就是我的模型: class Question < ActiveRecord::Base enum question_type: [ :multiple_choice ] validates :question_type, presence: true end 这是错误: Failure/Error: expect(build(:question, question_type: :telepathy)).to have(1).errors_on(:question_type) ArgumentError: ‘telepathy’ is […]

Bundler找不到rake但似乎安装了

我正在与捆绑和耙子进行一些斗争。 根据错误消息,bundler找不到rake-10.3.1。 这就是事情,我所做的一切似乎表明安装了rake-10.3.1。 我已经漂浮在谷歌领域好几天没有找到解决方案来解决这个问题。 我在Mac OSX 10.8.5上。 Ruby版本是1.9.3-p392。 错误: bundle exec rspec spec / bdr_pre_dev_spec.rb /Users/dru.solis/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.6.2/lib/bundler/spec_set.rb:92:in` materialize’:无法从/Users/dru.solis/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.6找到任何来源(Bundler :: GemNotFound)中的rake-10.3.1 .2 / lib / bundler / spec_set.rb:85:在`map!’ 来自/ Users / dr。来自/Users.ru .solis / .rvm / gems / ruby​​-1.9.3-p392 @ global / gems / bundler-1.6.2 / lib / bundler / definition.rb:133:来自/Users/dru.solis/.rvm的`specs’ /gems/ruby-1.9.3-p392@global/gems/bundler-1.6.2/lib/bundler/definition.rb:178:来自/Users/dru.solis/.rvm/gems/ruby-的`specs_for’ 1.9.3-p392@global/gems/bundler-1.6.2/lib/bundler/definition.rb:167:来自/Users/dru.solis/.rvm/gems/ruby-1.9.3-p392的`requested_specs’ @ global / gems […]

在RSpec 2中使用OR的平等

编写以下示例的正确方法是什么? 球员的得分应该等于5或8。 it “should equal 5 or 8” do player.score.should == 5 or 8 end 谢谢! 蒂姆

在第一次失败时停止执行特定的rspec测试(不是测试套件)

有没有一些程序化的方法来强制rspec测试在第一次失败时停止? 例如, 我有一个带有几个rspec测试的测试套件,我一次运行它们。 每个rspec测试都位于单独的spec.rb文件中 如果在任何这些测试中发生某些故障,我想在特定的测试步骤上停止执行该特定测试,该测试步骤失败但不会停止执行我正在运行的整个测试套件。 我知道有以下方法: RSpec.configure do |c| c.fail_fast = true end 但是,在我在测试套件中运行的任何规范文件中定义它会导致套件的所有执行失败。 这种情况有什么办法可以处理? 提前致谢

我可以在rspec中使用多个排除filter吗?

在_spec.rb文件中,我正在设置一个排除filter,如: RSpec.configure do |config| # we need determine this once at the very front # and the result be available in the instance server_success = server1_available? config.exclusion_filter = { :svr1 => lambda {|what| case what when :available !server_success end } } end 然后在我做的文件中 describe :get_items_by_client, :svr1 => :available do 如果服务器不可用,则阻止测试执行。 如果我单独运行spec文件,这一切都正常。 但是,我在控制访问不同服务器的测试的另一个文件中有类似的代码,当我运行它们时,我只看到每个服务器检查完成(我有一个放入“serverX_available?”代码),但只有正在排除一组测试(即使两个服务器都不可用)。 我开始认为你只能有一个排除filter,但我可以找到任何与之相关的文档。 这是可行的每个文件? 我可以在支持文件中使用单个复杂的filter,但是当我只运行单个spec文件时,如何将其合并? […]

尝试使用RSpec测试OmniAuth时出现“禁用真实HTTP连接”错误

我一直试图用RSpec测试OmniAuth,但它还没有奏效。 在spec_helper.rb OmniAuth.config.test_mode = true OmniAuth.config.add_mock(:twitter, {:uid => ‘12345’}) 并在spec/requests/static_pages_spec.rb describe “for signed-in users” do before do visit “auth/twitter” end it { should have_content(“Log out”) } end 我得到以下错误。 Failure/Error: visit “auth/twitter” ActionView::Template::Error: Real HTTP connections are disabled. Unregistered request: 根据官方文档,对auth/twitter的请求应该重定向到auth/twitter/callback 。 为什么尝试进行HTTP连接? 我已经阅读了以下网页和问题,但我找不到测试失败的原因。 http://blog.zerosum.org/2011/03/19/easy-rails-outh-integration-testing.html https://github.com/intridea/omniauth/wiki/Integration-Testing omn​​iauth-facebook和测试rspec http://blog.plataformatec.com.br/2010/12/acceptance-tests-for-omniauth/