2015-12-01 37 views
-1

這是我的第一個問題,我實際上在問,因爲我很絕望,我的課程的導師不在了......我正在關注一個web開發課程並遇到了這個問題使用RSpec進行單元測試的問題...我使用的命令行是rspec spec/models(並且該文件夾中的文件是:product_spec.rb)。這是我使用的測試代碼:這個rspec失敗代碼有什麼問題

require 'rails_helper' 

    describe Product do 

     context "when the Product has comments" do 

      before do 
       @product = Product.create(:name => "product model") 
      end 
     end 

     context "when user has email & password" do 

      before do 
       @user = User.create(:email => "[email protected]", :password => "passtheword") 
      end 
     end 



    context "create comment 1" do 

     before do 
      @product.comments.create(:rating => 1, :user => @user, :body => "Awful comment!") 
     end 
    end 

    context "create comment 2" do 

     before do 
      @product.comments.create(:rating => 3, :user => @user, :body => "another awful comment!") 
     end 
    end 

    context "create coment 3" do 

     before do 
      @product.comments.create(:rating => 5, :user => @user, :body => "yet another awful comment!") 
     end 
    end 

    context "rating average" do 
     before { @average_rating = Product.comments.count(:rating) } 
     it "returns the average rating" do 
      expect(@average_rating).to eq 3 
     end 
    end 

    end 

這是我從RSpec的發現了錯誤:

F 

    Failures: 

     1) Product rating average returns the average rating 
     Failure/Error: before { @average_rating = Product.comments.count(:rating) } 

NoMethodError: 
    undefined method `comments' for #<Class:0x007fe22e58b6d8> 
# ./spec/models/product_spec.rb:43:in `block (3 levels) in <top (required)>' 

Finished in 0.00099 seconds (files took 1.61 seconds to load) 
1 example, 1 failure 

Failed examples: 

rspec ./spec/models/product_spec.rb:44 # Product rating average returns the average rating 

我甚至嘗試宣告的「評論」像這樣:

context "register comment" do 
    before do 
    @comments = @product.comments 
    end 
end 

但是這沒有什麼區別。任何關於此事的幫助將非常感激,因爲我瘋了,並在過去的3天中在計算機附近砸碎了東西!接下來的事情是粉碎電腦本身! 非常感謝您的閱讀。希望你過得愉快。

+0

哦!忘了補充一點,我應該測試提交的評論的平均評分,但不要使事情複雜化,在現階段,如果代碼返回提交了多少評論/評分,我會很高興;因此= 3仍然有效,因爲評分1 + 3 + 5/3仍然給出3.謝謝。 – TYX

回答

0

不理解你的域名,模型關聯等等,很難清楚。不過,您的主要問題似乎是使用上下文塊。

在上述每個上下文塊context "create comment 1" do中設置上下文,使用before塊設置測試,然後關閉上下文塊。這些塊中的每一塊都是彼此不同的,因爲它們目前的編寫沒有任何目的 - 因爲沒有期望在任何一個塊中進行測試。

context "rating average" do 
    ##This sets up your test within this context block. 
    before do 
    Product.comments.create(:rating => 1, :user => @user, :body => "Awful comment!") 
    Product.comments.create(:rating => 3, :user => @user, :body => "another awful comment!") 
    Product.comments.create(:rating => 5, :user => @user, :body => "yet another awful comment!") 
    end 

    ##Now lets test it. 
    let(:average_rating) { Product.comments.count(:rating) } 

    it "returns the average rating" do 
    expect(average_rating).to eq 3 
    end 
end 

該代碼可能不正確,但應該更清楚地顯示如何構建測試。您可能需要進一步調整。

下面的代碼需要的餐館和評論可以被比喻爲產品和評論/評級類似的例子:

##In this example we need to create some users to create our restaurants and then reviews so use RSpec's ```let``` which is lazily evaluated. 
let(:user) { User.create(email: "[email protected]", password: "password" } 
let(:user2) { User.create(email: "[email protected]", password: "passwordpass2" } 

##Then we test (and also set up the conditions within the test itself). 
context '1 review' do 
    it 'returns that rating' do 
    restaurant = user.restaurants.create(name: 'BK Lounge') 
    restaurant.build_review({"thoughts" => "Great!", "rating" => 4}, user).save 
    expect(restaurant.average_rating).to eq 4 
    end 
end 

context 'multiple reviews' do 
    it 'returns the average' do 
    restaurant = user.restaurants.create(name: 'BK Lounge') 
    restaurant.build_review({"thoughts" => "Not great!", "rating" => 1}, user).save 
    restaurant.build_review({"thoughts" => "Great!", "rating" => 5}, user2).save 
    expect(restaurant.average_rating).to eq 3 
    end 
end 

值得一提的是,這些測試可能是嚴重重構的決策無論是letbefore甚至使用諸如FactoryGirl之類的工具來創建我們的測試對象,但應該爲解決您的問題提供一個有用的示例。

希望這會有所幫助。

+0

非常感謝您的時間和幫助;我非常感激。雖然它沒有解決問題,但我仍然設法完全理解它是如何工作的......你放下它的方式,幾乎可以瞬間明白我在哪裏得到這些反覆出現的問題。 – TYX

+1

它沒有解決問題的唯一原因是因爲我犯了一個非常愚蠢的錯誤,並沒有檢查模型的驗證!有4個對象需要驗證,我只是給它1 ... doh !!!!霍克先生非常感謝您的時間;非常感謝!希望你過得愉快:) – TYX