lundi 5 août 2019

How to translate default messages in Rspec?

I am creating some tests with Rspec to validate that some exercises done in plain Ruby are valid and for that I need to know if it is possible to translate some messages that Rspec throws by default. For example:

I have a custom matcher called should_have_attr_accessor.rb and it has the following:

RSpec::Matchers.define :have_attr_accessor do |field|
  match do |object_instance|
    object_instance.respond_to?(field) &&
      object_instance.respond_to?("#{field}=")
  end

  failure_message do |object_instance|
    "se espera un attr_accessor llamado #{field} en #{object_instance}"
  end

  failure_message_when_negated do |object_instance|
    "se espera un attr_accessor llamado #{field} no definido en #{object_instance}"
  end

  description do
    "afirmar que hay un attr_accessor de un nombre dado en el objeto proporcionado"
  end
end

and I have my spec like this:

require "spec_helper"
require_relative "../lib/cuenta_bancaria"

describe CuentaBancaria do
  context "cuando es inicializada con cantidad de argumentos correctos" do
    subject { CuentaBancaria.new("Gonzalo", 12345678 ) }
    it { expect(subject).to   have_attr_accessor(:nombre_de_usuario) }
  end
end


The output of Rspec is as follows:

➜ rspec spec/cuenta_bancaria_spec.rb 

CuentaBancaria
  cuando es inicializada con cantidad de argumentos correctos
    should afirmar que hay un attr_accessor de un nombre dado en el objeto proporcionado

I want to translate “should” to “debería”

PD: I know I could leave it all in English and students should learn it, but I want to know if it is possible to translate the requested.

Aucun commentaire:

Enregistrer un commentaire