我真的很困惑。我在自动化测试套件中使用下面的 Ruby 类:
class FlightSearchPage
attr_accessor :page_title
def initialize(browser, page)
@browser = browser
@start_url = page
end
def method_missing(sym, *args, &block)
@browser.send sym, *args, &block
end
@page_title = @browser.title
#def page_title
# @browser.title
#end
end
我将其子类化为:
class BrandFlightsPage < FlightSearchPage
def initialize(browser, page, brand)
super(browser, page)
[...snip...]
end
end
当我实例化子类时,传入 @browser,一个 watir-webdriver 对象,@browser.title 抛出:
nil:NilClass (NoMethodError) 的未定义方法“标题”
但是,如果我取消注释 FlightSearchPage 中的 page_title 方法,它会按预期工作。我也尝试过构造函数中的变量赋值。
我绕着圈子试图追踪它,但我就是看不到它。
这是堆栈跟踪:
undefined method `title' for nil:NilClass (NoMethodError)
/Users/Dazzla/Dropbox/src/Projects/over_the_watir/pages/flight_search_page.rb:13:in `<class:FlightSearchPage>'
/Users/Dazzla/Dropbox/src/Projects/over_the_watir/pages/flight_search_page.rb:1:in `<top (required)>'
/usr/local/rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require'
/usr/local/rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require'
/Users/Dazzla/Dropbox/src/Projects/over_the_watir/pages/brand_flight_search_page.rb:1:in `<top (required)>'
/usr/local/rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require'
/usr/local/rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require'
/Users/Dazzla/Dropbox/src/Projects/over_the_watir/features/step_definitions/steps.rb:1:in `<top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/rb_support/rb_language.rb:129:in `load'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/rb_support/rb_language.rb:129:in `load_code_file'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/runtime/support_code.rb:171:in `load_file'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/runtime/support_code.rb:83:in `block in load_files!'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/runtime/support_code.rb:82:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/runtime/support_code.rb:82:in `load_files!'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/runtime.rb:175:in `load_step_definitions'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/runtime.rb:40:in `run!'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/cli/main.rb:43:in `execute!'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/lib/cucumber/cli/main.rb:20:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p0/gems/cucumber-1.2.1/bin/cucumber:14:in `<top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p0/bin/cucumber:19:in `load'
/usr/local/rvm/gems/ruby-1.9.3-p0/bin/cucumber:19:in `<main>'