Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby TestCase:在“self.startup”方法中定义实例变量_Ruby_Unit Testing_Selenium_Oop_Inheritance - Fatal编程技术网

Ruby TestCase:在“self.startup”方法中定义实例变量

Ruby TestCase:在“self.startup”方法中定义实例变量,ruby,unit-testing,selenium,oop,inheritance,Ruby,Unit Testing,Selenium,Oop,Inheritance,我有一个Ruby代码: class GoogleTestCase

我有一个Ruby代码:

class GoogleTestCaseend
错误是因为您试图访问在类级别定义的实例变量
@browser
。因为
startup
shutdown
是类方法,
@browser
相应地是类变量

您可以使用
@@browser
从实例级别访问类变量

class GoogleTestCase < BaseTestCase

    def test_search
        @@browser.find_element(:name, 'q').send_keys "Hello Ruby"
        @@browser.find_element(:name, 'btnK')
    end

end

您可以使用实例方法
setup
teardown
,而不是类方法
startup
shutdown
。或者使用浏览器,即
@@browser
。不,我不能。因为
setup
方法的行为不同于
startup
方法<代码>启动只调用一次,但在类的每个测试方法之前都会调用
设置
。我不想这样。”或者使用一个类变量“好的,谢谢。所以,如果我想使用实例变量(而不是类变量),是不是可能?我不确定我是否理解您试图解决的问题。类变量有什么问题?
class BaseTestCase < Test::Unit::TestCase
    def self.startup
        @browser = Selenium::WebDriver.for :chrome
        @browser.get('https://google.com')
    end

    def self.shutdown
        @browser.quit
    end

    def browser
      @@browser
    end
end

class GoogleTestCase < BaseTestCase

    def test_search
        browser.find_element(:name, 'q').send_keys "Hello Ruby"
        browser.find_element(:name, 'btnK')
    end

end