2013-03-10 80 views
0

我使用的Rails版本3.2.12,我有一些問題,我的集成測試(單元和功能測試工作完美)。Ruby on Rails的 - 集成測試 - 未定義的方法「獲得」

我使用下面的命令來創建集成測試:

rails generate integration_test user_stories 

然後,我已經填充了「user_stories_test.rb」文件如下:

require 'test_helper' 

class UserStoriesTest < ActionDispatch::IntegrationTest 

    LineItem.delete_all 
    Order.delete_all 
    ruby_book = product(:ruby) 

    get "/" 
    assert_response :success 
    assert_template "index" 

    xml_http_request :post, '/line_items', product_id: ruby_book.id 
    assert_response :success 
    cart = Cart.find(session[:cart_id]) 
    assert_equal 1, cart.line_items.size 
    assert_equal ruby_book, cart.line_items[0].product 

    get "/orders/new" 
    assert_response :success 
    assert_template "new" 

    post_via_redirect "/orders", 
        order: { name: "Dave Thomas", 
          address: "123 The Street", 
          email: "[email protected]", 
          pay_type: "Check" } 
    assert_response :success 
    assert_template "index" 
    cart = Cart.find(session[:cart_id]) 
    assert_equal 0, cart.line_items.size 

    orders = Order.all 
    assert_equal 1, orders.size 
    order = orders[0] 
    assert_equal "Dave Thomas", order.name 
    assert_equal "123 The Street", order.address 
    assert_equal "[email protected]", order.email 
    assert_equal "Check", order.pay_type 
    assert_equal 1, order.line_items.size 
    line_item = order.line_items[0] 
    assert_equal ruby_book, line_item.product 

    mail = ActionMailer::Base.deliveries.last 
    assert_equal ["[email protected]"], mail.to 
    assert_equal 'Sam Ruby <[email protected]>', mail[:from].value 
    assert_equal "Pragmatic Store Order Confirmation", mail.subject 

end 

這就是我如何「test_helber。 rb「文件看起來像:

ENV["RAILS_ENV"] = "test" 
require File.expand_path('../../config/environment', __FILE__) 
require 'rails/test_help' 

class ActiveSupport::TestCase 
    fixtures :all 
end 

首先,我的燈具沒有加載,而且之後我是不是能夠手動修正類似這樣下面these說明我已經結束了創建測試數據:

#ruby_book = product(:ruby) 

    ruby_book = Product.new 
    ruby_book.title = 'Programming Ruby 2.0' 
    ruby_book.price = 49.50 
    ruby_book.image_url = 'ruby.png' 
    ruby_book.description = 'description' 
    ruby_book.save! 

這解決了燈具上裝載現在我得到「方法未定義」錯誤的問題我「搞定」方法。更多的是,在我評論所有「get」方法後,我在下一個方法 - 「xml_http_request」上遇到同樣的錯誤。

我做錯了什麼?爲什麼Rails無法找到這些方法?

回答

1

你是不是在測試的情況下,當你運行該代碼。

一個測試::單位的情況下將具有以下格式:

require 'test_helper' 

class UserStoriesTest < ActionDispatch::IntegrationTest 
    def test_it_works_as_it_should # <-- this is the part you're missing! 
    # Your test code goes here 
    end 
end 

只需將代碼爲您的測試在類並沒有做所有的測試::單位預計爲您正確的安裝測試以正確運行。

有關在Rails中測試的更多信息: http://guides.rubyonrails.org/testing.html#integration-testing

相關問題