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
How to Share Code Across Chef Cookbooks in a Chef-Repo
Parsing a CSV File Using Different Encodings and Libraries
Using Ruby with Mechanize to Log into a Website
What Is 'stringify_Keys' in Rails and How to Solve It When This Error Comes
Why Does The Ruby Module Kernel Exist
Paperclip and Amazon S3 How to Do Paths
Is It Still Advisable to Test Routes in Rails 4 with Minitest
Changing The Reading Order of Rubygem Sources
Set Ruby 2.0 Keyword Arguments with Attr_Accessor on Initialize
Resizing an Image with Mini_Magick
Can Optionparser Skip Unknown Options, to Be Processed Later in a Ruby Program
How to Stub/Mock a Call to The Command Line with Rspec
Obtaining Number of Block Parameters
Crontab Not Running Ruby Script
Rails for Zombies Lab 4 > Exercise 3
Using Rails with Paperclip and Swfupload
Broken Rails Routes After Implementing Single Table Inheritance