我听说最好避免在RSpec中使用its,let,let!,specify,before,和subject。关于let,let!和before,如果不使用它们,我该如何执行设置任务? 最佳答案 编写这些最佳实践的人实际上只想说“不要使用RSpec”。避免使用核心功能并仍然认为你可以把事情做好是很疯狂的。你能链接到这些最佳实践吗?我很想阅读它们,看看它们是否合理,或者只是一些j-random-guy的意见。避免before和let?什么?真的吗?避免specify,我想,这只是it的旧语法。避免特定的subject电话?我想,如果您的代
我无法理解为什么在运行我的测试时,以下代码在rspec中不起作用。Lead.update_status(2,2,true)expect(Lead).toreceive(:update_status).with(2,2,true)我也试过Lead.update_status(2,2,true)expect_any_instance_of(Lead).toreceive(:update_status).with(2,2,true)我得到的错误是().update_status(2,2,true)expected:1timewitharguments:(2,2,true)received:0
我正在使用Rspec和Capybara编写集成测试。我注意到,在测试activerecord选项的创建时,我经常必须执行相同的代码。例如:it"shouldcreateanewinstance"do#Icreateaninstancehereendit"shoulddosomethingbasedonanewinstance"do#Icreateaninstancehere#Iclickintotherecordandaddasubrecord,orsomethingelseend问题似乎是ActiveRecord对象不会在测试中保持不变,但默认情况下,Capybara在规范中维护相同
我的代码只需要在特定版本的ActiveRecord上运行(解决旧AR库上的错误的方法)。此代码测试ActiveRecord::VERSION常量的值以查看它是否需要运行。有没有办法在rspec中模拟这些常量,这样我就可以测试该代码路径,而无需在测试机器上安装正确的ActiveRecordgem? 最佳答案 我最终编写了一个辅助方法来让我在执行代码块时覆盖常量:defwith_constants(constants,&block)constants.eachdo|constant,val|Object.const_set(consta
我使用rspec-guard连续运行我的测试,但有时测试失败是因为:Running:spec/requests/signup_spec.rbFFFailures:1)SignupdoesnotemailconfirmationmailaftersignupFailure/Error:visitnew_user_pathNameError:undefinedlocalvariableormethod`new_user_path'for##./spec/requests/signup_spec.rb:4:in`block(2levels)in'2)Signupshouldemailconf
我遇到了一个奇怪的问题。我以前从未使用过Devisegem。所以,我尝试编写简单的RSpec测试:require'spec_helper'describe"Admins"dobefore(:each)do@email="test@test.ru"user=FactoryGirl.create(:user,:email=>@email)enddescribe"signin/out"dodescribe"success"doit"shouldsignauserinandout"dovisitnew_user_session_pathendendendend我收到这样的错误:1)Admins
我从Webrat迁移到Capybara,现在我遇到了很多错误。例如在webrat中我可以在集成测试中使用它:response.shouldbe_success但是Capybara表明:Failure/Error:response.shouldbe_successNoMethodError:undefinedmethod`success?'fornil:NilClass有没有提供这种功能的方法?UPD:我的规范:require'spec_helper'describe"Admins"dobefore(:each)do@admin=FactoryGirl.create(:admin)vis
我正在尝试为用户模型创建一个测试用例。基本上,它将验证first_name和last_name是否存在。我想做的是检查特定字段上的错误是否为空,并且应该为空。然而它总是失败。正确的做法是什么?这是我的代码在我的user_spec.rb上require'spec_helper'describeUserdobeforedo@user=User.newendit"musthaveafirstname"do@user.errors[:first_name].should_notbe_emptyendit"musthavealastname"do@user.errors[:last_name].
我有一个测试Controller的规范,如下所示require'spec_helper'describeProductsControllerdosetup:activate_authlogicdescribe"usernotloggedin"doit"shouldnotGETindex"doget:indexresponse.shouldredirect_to(login_path)endenddescribe"userloggedin"dobefore(:each)doUserSession.create:username=>"rohit",:password=>"test123"e
let的正常行为本质上是绑定(bind)到一个示例block(即它block)。这在大多数情况下都很好,但如果您碰巧在一个let中创建大型对象并用于多个实例,则可能会出现严重问题例子。在这种情况下,实例变量几乎成为测试套件完整性的必要条件。时间会这样复合:let=#在*对象创建时间调用的示例实例=对象创建时间在调用的示例数量超过3个的情况下,这个时间很快就会成为问题。大多数人会说使用实例变量是异端邪说,但就let的当前状态而言,这似乎是一种过于武断的胡言乱语,没有太多相反的研究。在我读过的任何支持let的文章中,唯一的论点是对延迟加载和作用域问题的一些吸引力,这两者在编写良好的测试中都