简体   繁体   English

Rspec和Rails 4,更新跳过回调

[英]Rspec and Rails 4, update skip callback

I try to run an update test with rspec for my rails application. 我尝试对我的Rails应用程序使用rspec运行更新测试。

I'm using rspec 3.5 and rails 4. 我正在使用rspec 3.5和rails 4。

The behaviour is supposed to be the following : 该行为应为以下情况:

When i create a new service with a selling price, it's create a Price instance and set the relation with the service. 当我创建带有售价的新服务时,它会创建一个Price实例并设置与该服务的关系。 Then, when i update my service, if there is no selling price, it's destroy the price record (requirement of the client to save space in database). 然后,当我更新我的服务时,如果没有售价,则会破坏价格记录(要求客户节省数据库空间)。 The process i implemented seems to be working fine, when i test with the UI and i check the count of Price record, it's decrease by one like it's suppose. 我执行的过程似乎运行良好,当我使用UI进行测试并检查Price记录的数量时,它的数量就减少了一个。 However, the unit test if failing. 但是,单元测试是否失败。

Here is the code : 这是代码:

Service Controller : 服务控制器:

  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

The callback in the Service model : Service模型中的回调:

  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

The test : 考试 :

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: "")

} }

As you can see, there is a lot of puts since i try to find what is wrong. 如您所见,自从我尝试找出问题所在以来,有很多看跌期权。

The output is : nombre de service avant création : 0 nombre de prix avant création : 0 in create or update price nombre de service après création : 1 nombre de prix après création : 1 nombre de prix avant update : 1 id 10 nombre de prix après update : 1 输出为:服务先行证书数量:0服务先行证书数量:0创建或更新价格服务先行证书数量:1服务先行证书数量:1服务先行证书更新:1 id 10服务先行证书数量:更新:1

Failure/Error: expect(lodging.reload.price).to be_nil 失败/错误:expect(lodging.reload.price).to be_nil

   expected: nil
        got: #<RegularPrice id: 2, label: nil, currency: "CHF", selling_price: 200.0, priceable_type: "Service", p...ated_at: "2017-07-13 08:08:47", quantity: 1, type: "RegularPrice", position: 1, purchase_price: nil>

As we can see, it's look like the callback is not fired after the update, and the action in the controller is not reached. 如我们所见,更新后似乎未触发回调,并且未达到控制器中的操作。

Have you any idea what is going wrong? 你知道出什么事了吗?

Thanks :) 谢谢 :)

PS: I always have trouble to include code in my questions, is there a tutorial on how to make it? PS:我总是很难在问题中包含代码,是否有关于如何编写代码的教程?

Since you did not mentioned your ruby version I'll assume it's 2. 由于您没有提到您的红宝石版本,因此我假设它是2。

First of all you need to learn how to properly debug your code in order to fix the issues yourself. 首先,您需要学习如何正确调试代码以自己解决问题。

Here is what you have to do: 这是您要做的:

1.Add pry gem to your app, pry-byebug there is also a version for ruby 1.9.3. 1.将pry宝石添加到您的应用程序, 撬byebug还有红宝石1.9.3的版本。

2.Add a break point in your code 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.Verify all the variable and their values and see where the problem lies 3.验证所有变量及其值,并查看问题所在

In case you could not find the issue with binding.pry in your rspec file before the expect add it to after the first line of the method, and you can step through the debugger by typing next in the console opened by pry (it will be where your rails server is runnig). 如果您在rspec文件中未找到与binding.pry有关的问题,然后将期望添加到该方法的第一行之后,则可以通过在pry打开的控制台中键入next来逐步调试程序(这将是您的Rails服务器在哪里运行)。 If that still does not help, try and add a binding.pry in your classes and see what is the state in there. 如果仍然不能解决问题,请尝试在类中添加binding.pry ,然后查看其中的状态。

Spend a few minutes/hours now to learn debugging and it will save you days/weeks in long term. 现在花几分钟/几小时来学习调试,这将为您长期节省几天/几周的时间。 (while you are learning you don't really know how a program should behave and that extra knowledge is priceless, after a while you only need a debugger for extremely complicated issues). (在学习时,您实际上并不知道程序的行为方式,并且额外的知识是无价的,过了一会儿,您只需要调试器即可解决极其复杂的问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM