测试将商品添加到购物车

时间:2016-09-08 09:54:15

标签: ruby-on-rails ruby rspec

我正在学习rails和rspec,我编写了一个具有简单购物车系统的应用程序。为了测试它,我写了一个请求规范,但是我遇到了一个我不太明白的错误,我的规范是:

  context 'add item to cart' do
    Given!(:product){FactoryGirl.create(:product)}
    Given {get "/"}
    When {xhr get "/line_items?product_id=#{product.id}"}
    Then { response.body.include? 'class="item_price"' }
  end

,给出的错误是:

 Failure/Error: When {xhr get "/line_items?product_id=#{product.id}"}

     ArgumentError:
       wrong number of arguments (given 1, expected 2+)

我对此很新,所以我不确定自己应该做些什么。

2 个答案:

答案 0 :(得分:0)

xhr期待更多参数:xhr "get", "/line_items?product_id=#{product.id}" 试试这个:

  context 'add item to cart' do
    Given!(:product){FactoryGirl.create(:product)}
    Given {get "/"}
    When {xhr "get", "/line_items?product_id=#{product.id}"}
    Then { response.body.include? 'class="item_price"' }
  end

答案 1 :(得分:0)

解决方案最终成为:

  context "add item to cart" do
    Given!(:product){FactoryGirl.create(:product)}
    Given {get "/"}
    When {xhr :post, "/line_items?product_id=#{product.id}"}
    Then { response.body.include? "item_price" }
  end