Skip to content

Navigation Menu

Sign up

Testing Wicked with RSpec

shicholas edited this page Feb 27, 2013 · 3 revisions

This is probably the not the most elegant solution, but I found a way to test Wicked with RSpec and Factory_Girl

In my application model that uses Wicked I have something like this:

class UserApplication < ActiveRecord::Base
 attr_accessor :current_step
 attr_accessible :first_name, :last_name, :email, :address_line_1, :address_line_2, :city, :zip
 validates :first_name, presence: true, length: { minimum: 2, maximum: 25 }, if: :step_name?
 validates :last_name, presence: true, length: { minimum: 2, maximum: 25 }, if: :step_name?
 validates :address_line_1, presence: true, length: { maximum: 150 }, if: :step_address?
 validates :city, presence: true, length: { maximum: 25 }, if: :step_address?
 validates :state, presence: true, length: { maximum: 2 }, if: :step_address?
 validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, if: :step_email?
 def step_name?
 current_step == :name
 end
 def step_address?
 current_step == :address
 end
 def step_email?
 current_step == :email
 end
end

Basically a bunch of validations that get triggered depending on the current_step that the wizard form is on. I have three steps in my wizard: name, address and email.

I found success a factory like this:

FactoryGirl.define do 
 factory :application do
 current_step :email
 first_name 'Dirk'
 last_name 'Diggler'
 mobile_number '1234567890'
 email 'example@example.com'
 address_line_1 '123 Maple Street'
 address_line_2 ''
 dob '01/01/2001'
 ssn '123456789'
 city 'Las Vegas'
 app_password 'abcd1234A'
 end
end

Because I now defined current_step in my factory I can now set and do validations like this:

describe Application, focus: true do
 it "should have a valid factory" do
 build(:application).should be_valid
 end
 
 context "validations" do
 describe "application_steps/name" do
 subject(:user) {create(:application, current_step: :name)}
 it "should validate first name with a 2-35 letter string" do
 ["", "a", "I"].each do |word|
 user.first_name = word
 user.should_not be_valid
 end
 user.first_name = "ab"
 user.should be_valid
 end
 it "should validate last name with a 2-35 letter string" do
 user.last_name = ""
 user.should_not be_valid
 end
 end
 end
 describe "application_steps/address" do
 subject(:user) {create(:application, current_step: :address)}
 it "should validate address" do
 ["", "a", "I"].each do |word|
 user.address = word
 user.should_not be_valid
 end
 user.address = "ab"
 user.should be_valid
 end
 end
 end
end

And so on. Obvi my tests aren't complete, but at least I got the gist of testing validations pending where the forms are in my Wicked wizard.

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /