Tag: 顽固

我应该如何使用RSpec全局存根方法?

我正在研究Rails应用程序。 我试图在全局范围内存根方法。 我正在做的是将它存储在RSpec配置中,在before(:suite)块上,如下所示: RSpec.configure do |config| config.before(:suite) do allow_any_instance_of(MyModel).to receive(:my_method).and_return(false) end end 但是,启动测试失败,并出现以下错误: in `method_missing’: undefined method `allow_any_instance_of’ for # (NoMethodError) 任何线索? 我应该如何使用RSpec全局存根方法? P.

摩卡和嵌套对象

如果这是一个愚蠢的问题,我很嘲笑。 我可以用mocha做以下事情: person.expects(:first_name).returns(‘David’) 如何模拟嵌套对象? 假设我有一个属于一个人的产品,我想得到那个人的名字。 在我的应用程序中,我可能会这样做: product.person.first_name 如何使用模拟获得相同的结果?

我应该在测试时在Factory girl或spec文件中存根模型吗?

几乎每一个我遇到的spec文件最终都会写出如下内容: before :each do @cimg = Factory.build :cimg_valid @cimg.stub(:validate_img).and_return true @cimg.stub(:validate_img_url).and_return true @cimg.stub(:save_images).and_return true @cimg.stub(:process_image).and_return true @cimg.stub(:img).and_return true end 我的意思是,我从Factory.build获得的模型是完全有效的。 但是,如果我不存储那些东西,它会保存文件系统中的东西,并validation我没有测试的东西…… 我的意思是,我认为做这样的事情会更干净: before :each do @cimg = Factory.build :cimg_for_testing_tags end 如果甚至可以在工厂内进行存根。 存根模型的正确方法是什么?