在控制器测试中创建操作不起作用

时间:2016-07-20 12:35:27

标签: ruby-on-rails

在控制器测试中创建操作测试不能正常工作,并且没有提供问题的原因。

 FAIL["test_should_create_invoice", InvoicesControllerTest, 2016-07-20 14:18:44 +0200]
 test_should_create_invoice#InvoicesControllerTest (1469017124.50s)
        "Invoice.count" didn't change by 1.
        Expected: 2
          Actual: 1
        test/controllers/invoices_controller_test.rb:20:in `block in <class:InvoicesControllerTest>'

对象参数来自实际的帖子请求,工作正常。我只更改了client_id和seller_id参数,以便从灯具中获取它们。是否可以检查此帖子请求在测试环境中无效的原因?

invoices_controller_test.rb

  test "should create invoice" do
    assert_difference('Invoice.count') do
      post :create, invoice: {
          date: "2016-07-07",
          invoice_name_attributes: {
            "number"=>"9",
            "month"=>"7",
            "year"=>"2016"
          },
          place: "Szczecin",
          seller_id: clients(:client_google).id,
          client_id: clients(:client_microsoft).id,
          client_name: "Nazwa",
          client_street: "Ulica",
          client_zip: "23-232",
          client_city: "Miasto",
          client_country: "Polska",
          client_email: "test@example.pl",
          client_phone: "732-320-322",
          invoice_items_attributes:
          {
            "0" => {
              item_id: "2",
              quantity: "1",
              unit_price: "1.30",
              tax_rate: "23",
              net_price: "1.30",
              value_added_tax: "0.30",
              total_selling_price: "1.60",
              _destroy: "false"
            }
          },
          net_price: "1.30",
          value_added_tax: "0.30",
          total_selling_price: "1.60",
          total_price_in_words: "jeden euro 60/100",
          currency_rate_table_name: "129/A/NBP/2016",
          currency_rate_name: "EUR",
          currency_rate: "4.4469"
        }
    end

    assert_redirected_to invoice_path(assigns(:invoice))
  end

此帖子请求在dev and production environtemtn

中正常运行
Started POST "/invoices" for 127.0.0.1 at 2016-07-20 14:37:09 +0200
Processing by InvoicesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"tgTMZS15vBKkzadPrjgIatcxoi5CgFU79St5UYDDbyo=", "invoice"=>{"date"=>"2016-07-20", "invoice_name
_attributes"=>{"number"=>"3", "month"=>"7", "year"=>"2016"}, "place"=>"Warszawa", "seller_id"=>"2", "client_id"=>"1", "client_name"=>"Karol", "cl
ient_street"=>"Cicha", "client_zip"=>"71-100", "client_city"=>"Warszawa", "client_country"=>"Polska", "client_email"=>"test@gmail.c
om", "client_phone"=>"", "invoice_items_attributes"=>{"0"=>{"item_id"=>"2", "quantity"=>"1", "unit_price"=>"2", "tax_rate"=>"23", "net_price"=>"2
.00", "value_added_tax"=>"0.46", "total_selling_price"=>"2.46", "_destroy"=>"false"}}, "net_price"=>"2.00", "value_added_tax"=>"0.46", "total_selling_price"=>"2.46", "total_price_in_words"=>"dwa euro 46/100", "currency_rate_table_name"=>"138/A/NBP/2016", "currency_rate_name"=>"EUR", "curre
ncy_rate"=>"4.3811"}, "commit"=>"Create Invoice"}

1 个答案:

答案 0 :(得分:0)

我认为你需要发送诸如以下的参数:

post :create, params: { invoice: {...} }