2013-05-13 57 views
3

我正試圖讓下面的工作。不知道我失敗的地方。我收到以下錯誤:Ruby初學者試圖從Ruby中的另一個類調用一個方法?

1)錯誤:

test_google(Google): 
NoMethodError: undefined method `new' for Method:Class 
    google.rb:15:in `setup' 

我是新來的寶石,所以這是相當101有人能向我解釋我的錯誤,爲什麼讓我能理解。謝謝!

require "test/unit" 
require "selenium-webdriver" 
require "json" 
require "time" 
require_relative "methods" 

class Google < Test::Unit::TestCase 

    def setup 
    @driver = Selenium::WebDriver.for :firefox 
    @base_url = "https://www.google.com/" 
    @accept_next_alert = true 
    @driver.manage.timeouts.implicit_wait = 30 
    @verification_errors = [] 
    @search = Method.new() 
    end 

    def teardown 
    @driver.quit 
    assert_equal [], @verification_errors 
    end 

    def test_google 
    @driver.get(@base_url + "/") 
    @search.search 
    end 

end 

class Method 

    def search 
     @driver.find_element(:id, "gbqfq").clear 
     @driver.find_element(:id, "gbqfq").send_keys "this is a test" 
     @driver.find_element(:id, "gbqfb").click 
     @driver.find_element(:id, "gbqfb").click 
    end 

end 

我改變了類名:

require "test/unit" 
require "selenium-webdriver" 
require "json" 
require "time" 


class Google < Test::Unit::TestCase 

    def setup 
    @driver = Selenium::WebDriver.for :firefox 
    @base_url = "https://www.google.com/" 
    @accept_next_alert = true 
    @driver.manage.timeouts.implicit_wait = 30 
    @verification_errors = [] 
    @search = Suber.new() 
    end 

    def teardown 
    @driver.quit 
    assert_equal [], @verification_errors 
    end 

    def test_google 
    @driver.get(@base_url + "/") 
    @search.search 
    end 

end 


class Suber 


    def search 
     @driver.find_element(:id, "gbqfq").clear 
     @driver.find_element(:id, "gbqfq").send_keys "this is a test" 
     @driver.find_element(:id, "gbqfb").click 
     @driver.find_element(:id, "gbqfb").click 
    end 


end 

現在我不完全知道如何對付我的「蘇伯」類中設置@driver。我以爲它只是工作,但它拋出:

NoMethodError:未定義的方法find_element' for nil:NilClass google.rb:37:in搜索 ' google.rb:25:在`test_google'

:/

+0

我已經爲你的第二部分添加了答案,希望能對你有所幫助。 – 2013-05-13 19:44:26

回答

0

根據您的編輯做以下:

require "test/unit" 
require "selenium-webdriver" 
require "json" 
require "time" 

module Suber 


    def search 
     @driver.find_element(:id, "gbqfq").clear 
     @driver.find_element(:id, "gbqfq").send_keys "this is a test" 
     @driver.find_element(:id, "gbqfb").click 
     @driver.find_element(:id, "gbqfb").click 
    end 


end 

class Google < Test::Unit::TestCase 

    include Suber 

    def setup 
    @driver = Selenium::WebDriver.for :firefox 
    @base_url = "https://www.google.com/" 
    @accept_next_alert = true 
    @driver.manage.timeouts.implicit_wait = 30 
    @verification_errors = [] 
    #@search = Suber.new() 
    end 

    def teardown 
    @driver.quit 
    assert_equal [], @verification_errors 
    end 

    def test_google 
    @driver.get(@base_url + "/") 
    search 
    end 

end 
+0

它仍然會在那裏。他的'Method'類沒有繼承任何其他類,所以'search'實例方法沒有任何有關'@ driver'的知識。 – kiddorails 2013-05-13 19:24:05

+0

工作正常!非常感謝你! – Josh 2013-05-13 19:55:24

5

方法是Ruby中內置的類(http://ruby-doc.org/core-2.0/Method.html)您必須將類重命名爲其他類。

+0

謝謝!不過,現在我得到: NoMethodError:未定義的方法'find_element '的零:NilClass google.rb:35:在'搜索' google.rb:25:在'test_google」 所以,我覺得我還沒有正確地調用課程。我的Mothod類中的實例變量是否應該在Google中調用時才起作用?或者我需要在Method類中定義什麼? 'find_element'是我在Google中調用的selenium webdriver的內置方法。 – Josh 2013-05-13 19:22:45

+0

@ user2120790,你可以在你的問題中發佈新代碼嗎? – Dogbert 2013-05-13 19:23:43

+0

@ user2120790,並且在'find_element'中使用它之前,您沒有在新類上設置'@ driver'。 – Dogbert 2013-05-13 19:24:36

0

這真的不是一個解決方案,但如果你有時間,那麼Metaprogramming Ruby這本書會詳細講解課程。