我有一个非常具体的问题。我不想进行Controller测试,而是进行请求测试。而且我不想使用Capybara,因为我不想测试用户交互,而只想测试响应状态。我在spec/requests/api/garage_spec.rb下有以下测试require'spec_helper'describe"Garages"dodescribe"index"doit"shouldreturnstatus200"doget'http://api.localhost.dev/garages'response.status.shouldbe(200)response.body.should_notbe_emp
TLDR:App.count需要重新加载才能看到创建的记录。为什么?我找到了很多关于测试DELETE方法的引用资料,如下所示:expect{delete_request}.tochange(App,:count).by(-1)这是有道理的,并且适用于一些类似的场景。但是,我在测试不应该执行的删除时遇到了一个问题,例如当没有用户登录时。我从这里开始,用两种方法来测试同一件事:require'rails_helper'RSpec.describeV1::AppsController,type::controllerdolet(:user){create(:user)}let(:app){c
我的mock只有在如下所示的beforeblock中时才有效。这只是我对我的问题的快速而肮脏的表述。从字面上看,当我将行从beforeblock移动到doesnotquack断言时,它停止模拟:(describe'Ducks',type::featuredobeforedo...allow_any_instance_of(Duck).toreceive(:quack).and_return('bark!')visitanimal_farm_pathendcontext'isanoddduck'it'doesnotquack'doexpect(Duck.new.quack).toeq('
我正在对用户的提要进行分页,并想模拟我正在使用的API的响应。API可以返回奇怪的结果,所以我想确保如果API返回我已经看到的项目,请停止分页。我使用minitest在第一次调用方法get_next_page时stub,但我想在第二次和第三次用不同的值调用它时stub。我应该只使用rSpec吗?ruby新手...这是片段test"crawlerdoesnotpaginateifnonewitemsinnextpage"do#1:A,B#2:B,D=>D#3:A=>stopcrawler=CrawlJob.newfirst_page=[{"id"=>"item-A"},{"id"=>"i
我正在测试我的ControllerAction以供练习。在我的Controller中,我只想从我的数据库中按名称获取所有不同的产品:defshop@products=Product.select('distincton(name)*').sort_by&:orderend我已经手动检查过了,它工作正常。现在我正在使用我的RSpec设置我的测试,我想测试@products是一个大于0的数组:RSpec.describePagesController,type::controllerdodescribe'GET#shop'doit'shouldgetallproudcts'doget:sh
我有一个方法,其中有一个begin/rescueblock。如何使用RSpec2测试救援block?classCapturerdefcapturebeginstatus=ExternalService.callreturntrueifstatus=="200"returnfalserescueException=>eLogger.log_exception(e)returnfalseendendenddescribe"#capture"docontext"anexceptionisthrown"doit"shouldlogtheexceptionandreturnfalse"doc=C
我有两个类:1.Sale是ActiveRecord的子类;它的工作是将销售数据持久保存到数据库中。classSale2.SalesReport是一个标准的Ruby类;它的工作是生成和绘制有关销售的信息。classSalesReportdefinitialize(start_date,end_date)@start_date=start_date@end_date=end_dateenddefsales_in_durationSale.total_for_duration(@start_date,@end_date)end#...end因为我想使用TDD并且我希望我的测试运行得非常快,所
我创建了一个名为Transaction的服务对象,它处理下达的订单、设置付款,然后设置模型关联。这个类叫做Transaction,有两个方法,initialize和pay。我正在spec/services/中测试它(它在app/services中)。initialize方法接受一个account和用户传入的一些参数来处理订单。我正在尝试使用rspec测试pay。我实际上如何进行这样的测试?这个功能有很多事情要做。例如,它创建新模型,然后在它们之间建立一些关联。到目前为止,我创建了一个双重帐户,如下所示:@account=double("account",:confirmed=>true
我正在使用带有RSpec和Capybara的Rails4应用程序。我的gemfile看起来像:source'https://rubygems.org'gem'rails','4.0.0'gem'pg'gem'devise'gem'sass-rails','~>4.0.0'gem'uglifier','>=1.3.0'gem'coffee-rails','~>4.0.0'gem'jquery-rails'gem'jquery-turbolinks'gem'turbolinks'gem"rspec-rails",:group=>[:test,:development]group:testd
学习如何使用Rspec3。我对匹配器有疑问。我正在学习的教程基于Rspec2。describeTeamdoit"hasaname"do#Team.new("Randomname").shouldrespond_to:nameexpect{Team.new("Randomname")}.tobe(:name)endit"hasalistofplayers"do#Team.new("Randomname").players.shouldbe_kind_ofArrayexpect{Team.new("Randomname").players}.tobe_kind_of(Array)enden