草庐IT

rspec-stories

全部标签

ruby - rails rspec - 如何检查模型常量?

我该如何做:it{shouldhave_constant(:FIXED_LIST)}在我的模型(事件记录)中,我有FIXED_LIST='AString'它不是数据库属性或方法,我无法使用responds_to或has_attribute对其进行测试(它们失败了)。我可以用什么来检查它。-顺便说一句,我安装了shoulda-matchers。 最佳答案 根据DavidChelimsky的回答,我通过稍微修改他的代码使它起作用。在文件spec/support/utilities.rb(或spec/support中的其他文件)中,您可以

ruby-on-rails - Rspec:检查数组是否包含包含属性的对象

我有一个充满对象的json数组。my_array=[{id=>6,name=>"bob"},{id=>5,name=>"jim"},{id=>2,name=>"steve"}]我需要查看数组是否包含一个对象,该对象包含设置为5的属性“id”。“name”属性未知。我如何在rspec中执行此操作?我知道如果我有name属性我知道我可以这样做:my_array.shouldinclude({:id=>5,:name=>"jim"}) 最佳答案 expect(myArray.find{|item|item[:id]==5}).to_not

ruby-on-rails - rspec 在失败测试中的未决结果

it'shouldbeanarrayandnotbeempty'dopendingexpect(a.class).tobe(Array)expect(a.empty?).tobe(false)expect(a.first.class).tobe(ExampleClass)end当我运行rspec时:Failures:1)shouldbeanarrayandnotbeemptyFIXEDExpectedpending'Noreasongiven'tofail.NoErrorwasraised.#./spec/example_spec.rb:19知道为什么这会被列为失败吗?

ruby - 使用 Rspec 2 和 Mocha 获取 Rails 3 生成器

我已经按照我能够在网上找到的使用Rspec2和Mocha配置Rails3的所有步骤进行操作。在我的Gemfile中:group:developmentdogem'rails3-generators'gem"rspec",'>=2.0.0.beta.19'gem"rspec-rails",'>=2.0.0.beta.19'endgroup:testdogem"faker"gem"rspec",'>=2.0.0.beta.19'gem"rspec-rails",'>=2.0.0.beta.19'gem"machinist",'>=2.0.0.beta1'gem"mocha"gem"capy

ruby-on-rails - 如何构建我的 RSpec 测试文件夹、文件和数据库?

我已经使用RoR进行开发一年多了,但我才刚刚开始使用RSpec进行测试。对于标准模型/Controller测试,我通常没有任何问题,但问题是我想测试一些复杂的功能流程,并且不知道如何构建我的测试文件夹/文件/数据库。这是我的应用程序的基本结构:classCustomerhas_one:wallethas_many:ordershas_many:invoices,through::ordershas_many:invoice_summariesendclassWalletbelongs_to:customerendclassOrderhas_one:invoicebelongs_to:c

ruby-on-rails - RSpec 故事和规范 : When to use what?

所以我想开始使用RSpec故事,但我不确定编写Controller、模型和View规范的位置。例如,您有“登录”故事和“用户提供错误的密码”场景,难道您最终测试的不是与Controller/模型规范相同的东西(response.shouldrender...,user.shouldbe_nil等)所以我的问题是:对于那些习惯于使用RoR进行bdd(或故事dd)的人,您是否仍然编写模型/Controller规范?如果是这样,您遵循的工作流程如何(“第一个故事,然后缩小到特定规范”)? 最佳答案 如果您现在开始使用故事(而不是拥有大量遗

ruby-on-rails - RSpec - 模拟类方法

我正在尝试用rspec模拟一个类方法:lib/db.rbclassDbdefself.list(options)Db::Payload.list(options)endendlib/db/payload.rbclassDb::Payloaddefself.list(options={})endend在我的规范中,我正在尝试设置预期Db::Payload.list在我调用Db.list时将被调用:require'db/payload'describeDbdobefore(:each)do@options={}Db::Payload.should_receive(:list).with(@

ruby - 哪种风格,lambda..should 或 expect..to,更适合在 RSpec 中测试预期?

我看到两种样式都被广泛使用:#1lambda{raise"Boom"}.shouldraise_error和#2expect{raise"Boom"}.toraise_error.我喜欢expect..tomore,因为它读起来更好,并且隐藏了过程的创建。我查看了rspec代码,似乎expect..to是suggested,但是我经常遇到使用lambda..should的库。期望..更新,因此还不是“著名”吗? 最佳答案 expect从rspec-2开始使用,之前必须使用lambda。RSpec“正式”建议使用expect,他们可能

ruby - Rspec 模拟错误 : wrong number of arguments

我正在尝试使用Rspec对StripeAPI进行stub,但我遇到了一个问题。这是我的代码的样子:Stripe::Customer.should_receive(:create).with(any_args).and_raise(Stripe::CardError)这是我遇到的错误:Failure/Error:Stripe::Customer.should_receive(:create).with(any_args).and_raise(Stripe::CardError)ArgumentError:wrongnumberofarguments(0for3..6)

ruby-on-rails - 模拟 rspec 中的错误/异常(不仅仅是它的类型)

我有一段代码是这样的:defsome_methodbegindo_some_stuffrescueWWW::Mechanize::ResponseCodeError=>eife.response_code.to_i==503handle_the_situationendendend我想测试ife.response_code.to_i==503部分发生了什么。我可以模拟do_some_stuff以抛出正确类型的异常:whatever.should_receive(:do_some_stuff).and_raise(WWW::Mechanize::ResponseCodeError)但是我