International Chars Using Rspec with Ruby on Rails

International chars using RSpec with Ruby on Rails

Most likely you're missing a magic comment at the top of your file:

# encoding: UTF-8

Without this directive Ruby tries to interpret your file with the default US-ASCII encoding and fails since this character set does not include symbols like á or ç.

Here's a blog post on default source encoding in Ruby by James Edward Gray II.

How should I stub a method globally using RSpec?

It probably is a context / initialization issue. Doing it in config.before(:each) should solve your problem.

Iterating over certain model attributes with RSpec

I am not sure what your model attributes are (can you post them?) but if your model has the fields "protein" and "calories" and you validate these to be > -1 you probably want to:

  [:protein, :calories].each do |nutr|
subject[nutr] = -1
expect(subject).to_not be_valid
end

Also if you have two different validations defined in your model, it's better practice to spec them in two separate examples if you absolutely want to do it by the book.

It is a good test? rspec

In general, your specs are great. You check two normal cases that have different whitespace patterns that seem to be important to you. And you check two edge cases – an empty input and an input that is too long.

But I see any benefit in checking that the output is shorter than 140 chars before each example. All your examples have hard coded inputs and hard-coded, expected outputs anyway, and therefore you already know that none of them will be longer than 149 chars.

Additionally, I would refactor the specs a bit, for example, but that is certainly opinionated:

require 'rspec'
require './codewars/5kyu/hashtagGenerator'

describe '#generate_hashtag' do
subject(:hashtag) { generate_hashtag(input) }

describe 'with valid input' do
let(:input) { ' Hello there thanks for trying my Kata' }

it 'returns the expected formatted output' do
expect(hashtag).to eq('#HelloThereThanksForTryingMyKata')
end
end

describe 'with input with an unusually amount of whitespace' do
let(:input) { ' Hello World ' }

it 'returns the expected formatted output' do
expect(hashtag).to eq('#HelloWorld')
end
end

describe 'with empty input' do
let(:input) { '' }

it 'returns false' do
expect(hashtag).to be(false)
end
end

describe 'with an input being longer than 139 chars' do
let(:input) { "L" * 140) }

it 'returns false' do
expect(hashtag).to be(false)
end
end
end

Nitpicking: Ruby naming conventions for method names are that they should be written with underscores instead of camel case. Which means Ruby would prefer generate_hashtag over generateHashtag. Therefore, I already change the method naming in my above answer.



Related Topics



Leave a reply



Submit