'How should I test Rails Enums?
Suppose I have a model called CreditCard. Due to some technical restrictions, it's best if the card networks are represented as an enum. My code looks something like this:
class CreditCard < ActiveRecord::Base
enum network: [:visa, :mastercard, :amex]
end
What should be tested when using enums if anything?
Solution 1:[1]
According to Rails doc for Enum every value of the array correspond to an integer. I'm guessing you want to test that the network array always keeps its same order. If you're using rspec, you can do something like:
describe 'CreditCard' do
let(:network_values) do
{ visa: 0,
master: 1
# etc
}
end
subject { described_class.new }
it 'has valid a network' do
network_values.each do |type, value|
subject.network = value
subject.save
expect(subject.network).to eql(type.to_s)
end
end
end
ps: I'm writing from my mobile phone maybe you'll need some tweaking to run
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ale Paredes |
