Rspec和Rails 4,更新跳过回调

我尝试使用rspec为我的rails应用程序运行更新测试。

我正在使用rspec 3.5和rails 4。

行为应该是以下内容:

当我以销售价格创建新服务时,它会创建一个Price实例并设置与服务的关系。 然后,当我更新我的服务时,如果没有售价,则会破坏价格记录(客户要求节省数据库中的空间)。 我实施的过程似乎工作正常,当我用UI测试并且我检查价格记录的数量时,它会像它假设的那样减少一个。 但是,unit testing是否失败。

这是代码:

服务控制器:

def update @service.assign_attributes(service_params) puts "update method" respond_to do |format| if @service.valid? if params['preview'] @service.build_previews format.js { render 'services/preview' } else @service.save! format.html { redirect_to client_trip_days_path(@client, @trip), notice: t('flash.services.update.notice') } end else format.html { render :edit } format.js { render :new } end end end 

服务模型中的回调:

  def create_or_update_price puts "in create or update price" if selling_price.present? && price.present? self.price.update_columns(:trip_id => trip.id, :currency => trip.client.currency, :purchase_price => purchase_price, :selling_price => selling_price) elsif selling_price.present? && !price.present? self.price = RegularPrice.create(:trip => trip, :currency => trip.client.currency, :purchase_price => purchase_price, :selling_price => selling_price) elsif !selling_price.present? && price.present? self.price.destroy end end 

考试 :

 it "updates the lodging and destroy the price" do puts "nombre de service avant création : " puts Service.count puts "nombre de prix avant création : " puts Price.count lodging = FactoryGirl.create(:lodging_service, selling_price: 200) puts "nombre de service après création : " puts Service.count puts "nombre de prix après création : " puts Price.count expect(lodging.reload.price).to be_present puts "nombre de prix avant update : " puts Price.count puts "id" puts lodging.id patch :update, client_id: client.id, trip_id: trip.id, id: lodging.to_param, service: valid_attributes_no_more_price # patch :update, client_id: client.id, trip_id: trip.id, id: lodging.id, service: valid_attributes_with_price puts "nombre de prix après update : " puts Price.count # expect{ # patch :update, client_id: client.id, trip_id: trip.id, id: lodging.id, service: valid_attributes_no_more_price # }.to change(RegularPrice, :count).by(0) expect(lodging.reload.price).to be_nil end let(:valid_attributes_no_more_price) { attributes_for(:lodging_service, trip: trip, selling_price: "") 

}

正如你所看到的,自从我试图找出错误之后,有很多看跌期权。

输出是:nombre de serviceavantcréation:0 nombre de prixavantcréation:0 in create or update price nombre deserviceaprèscréation:1 nombre deprixaprèscréation:1 nombre de prix avant update:1 id 10 nombre deprixaprès更新:1

失败/错误:期望(lodging.reload.price)。到be_nil

  expected: nil got: # 

我们可以看到,看起来更新后没有触发回调,并且未达到控制器中的操作。

你知道出了什么问题吗?

谢谢 :)

PS:我总是很难在我的问题中包含代码,是否有关于如何制作代码的教程?

既然你没有提到你的ruby版本,我会假设它是2。

首先,您需要了解如何正确调试代码以便自己解决问题。

这是你要做的:

1.为你的应用程序添加pry gem, pry-byebug还有一个ruby 1.9.3的版本。

2.在代码中添加一个断点

 it "updates the lodging and destroy the price" do (...) # Code here. binding.pry # The debugger will open a console at this point (...) # Maybe more code here end 

3.validation所有变量及其值,并查看问题所在

如果你没有在你的rspec文件中找到binding.pry的问题,那么在期望之前将它添加到方法的第一行之后,你可以通过在pry打开的控制台中输入next来逐步调试调试器(它将是你的rails服务器运行的地方)。 如果仍然没有帮助,请尝试在类中添加binding.pry并查看其中的状态。

现在花几分钟/小时来学习调试,它可以为您节省数天/周的长期工作时间。 (当你在学习的时候,你真的不知道程序应该如何表现,而额外的知识是无价的,过了一段时间你只需要一个调试器来处理非常复杂的问题)。