我正試圖讓下面的工作。不知道我失敗的地方。我收到以下錯誤: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'
:/
我已經爲你的第二部分添加了答案,希望能對你有所幫助。 – 2013-05-13 19:44:26