Skip to content

Instantly share code, notes, and snippets.

@findchris
Created July 22, 2011 01:21
Show Gist options
  • Select an option

  • Save findchris/1098642 to your computer and use it in GitHub Desktop.

Select an option

Save findchris/1098642 to your computer and use it in GitHub Desktop.
Weird shoulda issue
bundle exec rspec spec/models/user_spec.rb
..FF.FF
Failures:
1) User validations
Failure/Error: it { should validate_presence_of(:secret) }
Expected errors to include "can't be blank" when secret is set to nil, got errors: ["name can't be blank (nil)"]
# ./spec/models/user_spec.rb:10:in `block (3 levels) in <top (required)>'
3) User validations
Failure/Error: it { should validate_uniqueness_of(:secret) }
Expected errors to include "has already been taken" when secret is set to "c96e36f3cc7aac38767cef6212cdab38", got errors: ["name can't be blank (nil)"]
# ./spec/models/user_spec.rb:13:in `block (3 levels) in <top (required)>'
# app/models/user.rb
class User < ActiveRecord::Base
validates :name, :uniqueness => true, :presence => true
validates :secret, :uniqueness => true, :presence => true
before_validation :assign_secret, :on => :create
private
def assign_secret
self.secret = SecureRandom.hex(16)
end
end
# spec/models/user_spec.rb
require 'spec_helper'
describe User do
context "validations" do
before(:each) { User.create!(:name => 'I am the First App') }
it { should validate_presence_of(:name) }
it { should validate_presence_of(:secret) }
it { should validate_uniqueness_of(:name) }
it { should validate_uniqueness_of(:secret) }
end
end
@cyrille
Copy link

cyrille commented Sep 12, 2011

hi I've the exact same problem. Did you manage to find a solution ?

@findchris
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment