我运行RSpec测试时的ActiveRecord :: RecordNotFound

时间:2014-10-01 19:34:15

标签: ruby-on-rails rspec

在视图/页面中,我在字段更改(无表单提交)上触发jQuery帖子以更新数据库表中的日期字段。这似乎在我的开发服务器中工作得很好,但是当我运行这个测试时,我得到一个ActiveRecord :: RecordNotFounderror,即使此语句之前/之后的计数返回1,我可以在FireFox中看到它是找到第一行并填写日期。

在测试日志中,我看到所有数据都是在对象实例化之前从订单行中删除的,这是导致此错误的原因,但我没有看到发生在我的其他测试中。这让我发疯,任何帮助都表示赞赏!

错误

Failures:

  1) Order Pages - WWhen a need by date is updated should update the line with the correct date
     Failure/Error: Unable to find matching line from backtrace
     ActiveRecord::RecordNotFound:
       Couldn't find OrderLine with id=1

RSPEC

require 'spec_helper'

describe "Order Pages - " do

  subject { page }

  before do
    create_order_and_sign_in_user 
  end

  def visit_a_order
    @order = @user.order.first
    visit order_lines_path(@order)
  end

  describe "When a need by date is updated" do

    it "should update the line with the correct date", js: true do
      visit_a_order  
         puts "Order lines count 1 - #{@order.lines.count}" // is 1 
      fill_in "needby_date", :with => "09/18/2014"
        puts "Order lines count 2 - #{@order.lines.count}" // is 1         
    end

  end  

end 

RSPEC帮助

module OrderHelper

  def create_order_and_sign_in_user

     @user = create(:user)
     @order = create(:order, user: @user)
     @order_line = @order.order_line.create(description: "Order 1, Line 1")

     sign_in @user

  end

end

include ApplicationHelper

  def sign_in(user)
   visit new_user_session_path
   fill_in "Email", with: user.email
   fill_in "Password", with: user.password
   click_button "Sign in"
  end

  def fixture_file_path(file_name)
   File.join(fixture_path, file_name)
  end

CONTROLLER

  def update_needby_date

    puts "Got to 1 - #{params[:order_line_id]}"
    @order_line = OrderLine.find(params[:order_line_id]) //Fails here with an ActiveRecord::RecordNotFound error
    puts "Got to 2 - #{params[:order_line_id]}"
    @order_line.needby_date = Date::strptime(params[:needby_date], "%m/%d/%Y")
    @order_line.save!
    respond_to do |format|
      format.js   { render :nothing => true }
    end

  end

TEST LOG

   [1m[36m (5.1ms)[0m  [1mDELETE FROM "order_lines";[0m
   [1m[35m (0.1ms)[0m  SELECT name FROM sqlite_master WHERE type='table' AND name='sqlite_sequence';
   [1m[36m (4.0ms)[0m  [1mDELETE FROM sqlite_sequence where name = 'order_lines';[0m

  Started POST "/order_lines/1/update_needby_date" for 127.0.0.1 at 2014-10-01 22:52:00 +0530
  Processing by OrderLinesController#update_needby_date as */*
    Parameters: {"needby_date"=>"09/18/2014", "order_line_id"=>"1"}
    [1m[35mOrderLine Load (0.3ms)[0m  SELECT "order_lines".* FROM "order_lines" WHERE "order_lines"."id" = ? LIMIT 1  [["id", "1"]]
**Completed 404 Not Found in 2ms**

1 个答案:

答案 0 :(得分:0)

我想所需要的只是来自现场的标签。所以我最终填写了同一页面上的另一个字段,让这个测试通过。

  describe "When a need by date is updated" do

     it "should update the line with the correct date", js: true do
      visit_a_order  
      fill_in "needby_date", :with => "09/18/2014"
      fill_in "line_description", :with => "Sample line!"
     end

  end