0

A slight problem I'm fairly new to Ruby, I have a class which contains instances of other classes(composition)

I'm trying to access the class instances within Cucumber, but keep returning a error undefined method `bk' for nil:NilClass (NoMethodError) 'bk' method is located inside the interior class I'm guessing this error is because Cucumber cannot access the class of Interior. Whats the best way of designing this or a suitable solution?

class CarConfig


def initialize(browser, wait)
@browser = browser
@wait = wait
@equipments = Equipment.new(@browser)
@interior = Interior.new(@browser)
@engines = Engines.new(@browser)
@exterior = Exterior.new(@browser)
@grades = Grades.new(@browser)
end

def click_comfort
@browser.find_element(:css, 'a.xdata-id-Comfort').click
end


def check_equipment
  equipment_availability = []
  equipment_not_available = " equipment not available"
  equipment_currently_available = "equipment available"

 equipment = [@equipments.lifestyle,@equipments.elegance, @equipments.comfort,   @equipments.executive, @equipments.luxury,
             @equipments.innova].each do

end
  equipment_availability.push equipment

 if "#{equipment_availability}".include? "disabled"
   equipment_not_available
 else
   equipment_currently_available
 end

end

 Cucumber 

 Given /^I have selected Comfort$/ do
 @car_configurator = CarConfig.new(@browser, @wait)
 @browser.get $car_config_page
 sleep(2)
 @car_configurator.click_comfort
 sleep(3)

 end

 Then /^I should see interior BK as available$/ do
 @interior.bk.should_not include ("disabled"), ("selected")
 end

1 Answer 1

1

Problem Simplified

The problem can be simplified to be seen without Cucumber (ie the problem is general ruby coding issue):

class Interior
    def bk()
        return 'bk method'
    end
end 

class CarConfig
    def initialize(browser, wait)
        @browser = browser
        @wait = wait
        @interior = Interior.new
    end
end

@car_configurator = CarConfig.new('browser', 'wait')
@interior.bk
#=> stuff.rb:16:in `<main>': undefined method `bk' for nil:NilClass (NoMethodError)

The problem is that @interior does not exist within the scope of the main (or in your case the cucumber step). It is only defined within the instance of the CarConfig - ie @car_configurator.

Solution

If you want to access the @interior, you need to create a method for this within the CarConfig. It is mostly easily done with an attribute accessor. The CarConfig class would have the following line added:

attr_accessor :interior

So that the class becomes:

class CarConfig
    attr_accessor :interior

    def initialize(browser, wait)
        @browser = browser
        @wait = wait
        @interior = Interior.new
    end
end

To then call the bk method of the @interior object, you would then need to access it starting from the @car_configurator:

@car_configurator.interior.bk
#=> "bk method"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.