mardi 30 juillet 2019

How to test initializer number of arguments in Ruby

I need to autograde a Ruby exercise , testing with Rspec

The class is pure ruby and I only have to validate attributes (existence and accessibility) and the number of arguments that are received in the initializer

# lib/card.rb
class Card
  attr_reader :number, :suit

  def initialize(number, suit)
    @number = number
    @suit = suit
  end
end

# spec/card_spec.rb
require "spec_helper"
require_relative "../lib/card"

describe Card do
  let(:subject) do
    Card.new(1, "D")
  end

  it { expect(Card).to respond_to(:new).with(4).arguments }
end


➜ rspec spec/card_spec.rb
.

Finished in 0.00431 seconds (files took 0.12135 seconds to load)
1 example, 0 failure

Tests are passing despite the fact that the initializer receives 2 arguments and I specify 4 arguments in my test

Aucun commentaire:

Enregistrer un commentaire