Why does ruby โ€‹โ€‹return `true` for a method call?

So this is a short tutorial I found at Rspec: w3ii.com: rspec about this explanation about rspec helpers. This is the code for the example:

class Dog
   attr_reader :good_dog, :has_been_walked

   def initialize(good_or_not)
      @good_dog = good_or_not
      @has_been_walked = false
   end

   def walk_dog
      @has_been_walked = true
   end
end

describe Dog do
   def create_and_walk_dog(good_or_bad)
      Dog.new(good_or_bad).walk_dog
   end

   it 'should be able to create and walk a good dog' do
      dog = create_and_walk_dog(true)

      expect(dog.good_dog).to be true
      expect(dog.has_been_walked).to be true
   end
end

      

When I run this I get this error:

NoMethodError: undefined method 'good_dog' for true: TrueClass
#. / Dor.rb: 22: in 'block <2 levels> in>'

I can't figure out how the call to Dog.new () returns true: a TrueClass object instead of a simple dog.

+3


source to share


2 answers


Which, since it create_and_walk_dog

returns, what the method walk_dog

returns

To return a dog from a method create_and_walk_dog

, you need something like

describe Dog do
   def create_and_walk_dog(good_or_bad)
      dog = Dog.new(good_or_bad)
      dog.walk_dog
      dog
   end

   it 'should be able to create and walk a good dog' do
      dog = create_and_walk_dog(true)

      expect(dog.good_dog).to be true
      expect(dog.has_been_walked).to be true
   end
end

      



Or continue with the wait block:

describe Dog do
   it 'should be able to create and walk a good dog' do
      dog = Dog.new(true)

      expect(dog.good_dog).to be true
      expect {
        dog.walk_dog
      }.to change(dog, :has_been_walked).from(false).to true
   end
end

      

+3


source


I think for clarity, you could / should have used the RSpec let method here is not the complete method definition included in your RSpec file.If your _walk_dog_ method returns itself as suggested in another answer, I see that your current one is fixed here RSpec implementation, but doesn't help you anywhere in your potential application. A hypothetical case where bad dogs return false for _dog.walk_dog_ randomly 50% of the time, or just don't cooperate at all.

class Dog
  attr_reader :good_dog, :has_been_walked

  def initialize(good_or_not)
    @good_dog = good_or_not
    @has_been_walked = false
  end

  # Command to attempt walking a dog.
  # 
  # @api public
  # 
  # @example
  #   good_dog = Dog.new(true)
  #   good_dog.walk_dog # => Will always return true for good dogs.
  #   bad_dog = Dog.new(false)
  #   bad_dog.wal_dog # => 50/50 chance of true or false
  def walk_dog
    return @has_been_walked = true if @good_dog
    [true, false].sample
  end
end

describe Dog do

  let(:good_dog) { Dog.new(true) }

  it 'should always be able to walk a good dog' do
    expect(good_dog.walk_dog).to be true
  end

  it 'should track if the dog has been walked' do
    expect {
      good_dog.walk_dog
    }.to change(dog, :has_been_walked).from(false).to true
  end

end

      



Forward for discussion as well, but you only have to assert one thing per spec, unless you hit the db or do something for a relatively long time.

PS They are all good dogs Brent

0


source







All Articles