Skip to content

Commit

Permalink
Use let() in rspec specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ugisozols committed Apr 21, 2011
1 parent 9cb46e4 commit fcdb18a
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 174 deletions.
78 changes: 36 additions & 42 deletions authentication/spec/models/user_spec.rb
Expand Up @@ -3,121 +3,118 @@
Dir[File.expand_path('../../../features/support/factories.rb', __FILE__)].each {|f| require f}

describe User do

let(:user) { Factory(:user) }
let(:refinery_user) { Factory(:refinery_user) }

context "Roles" do
context "add_role" do
it "raises Exception when Role object is passed" do
user = Factory(:user)
lambda{ user.add_role(Role.new)}.should raise_exception
end

it "adds a Role to the User when role not yet assigned to User" do
user = Factory(:user)
lambda {
user.add_role(:new_role)
}.should change(user.roles, :count).by(1)
user.roles.collect(&:title).should include("NewRole")
end

it "does not add a Role to the User when this Role is already assigned to User" do
user = Factory(:refinery_user)
lambda {
user.add_role(:refinery)
}.should_not change(user.roles, :count).by(1)
user.roles.collect(&:title).should include("Refinery")
refinery_user.add_role(:refinery)
}.should_not change(refinery_user.roles, :count).by(1)
refinery_user.roles.collect(&:title).should include("Refinery")
end
end

context "has_role" do
it "raises Exception when Role object is passed" do
user = Factory(:user)
lambda{ user.has_role?(Role.new)}.should raise_exception
end

it "returns the true if user has Role" do
user = Factory(:refinery_user)
user.has_role?(:refinery).should be_true
refinery_user.has_role?(:refinery).should be_true
end

it "returns false if user hasn't the Role" do
user = Factory(:refinery_user)
user.has_role?(:refinery_fail).should be_false
refinery_user.has_role?(:refinery_fail).should be_false
end
end

describe "role association" do
it "have a roles attribute" do
Factory(:user).should respond_to(:roles)
user.should respond_to(:roles)
end
end
end

context "validations" do
# email and password validations are done by including devises validatable
# module so those validations are not tested here
before(:each) do
@attr = {
let(:attr) do
{
:username => "RefineryCMS",
:email => "refinery@cms.com",
:password => "123456",
:password_confirmation => "123456"
}
end


it "requires username" do
User.new(@attr.merge(:username => "")).should_not be_valid
User.new(attr.merge(:username => "")).should_not be_valid
end

it "rejects duplicate usernames" do
User.create!(@attr)
User.new(@attr.merge(:email => "another@email.com")).should_not be_valid
User.create!(attr)
User.new(attr.merge(:email => "another@email.com")).should_not be_valid
end
end

describe ".find_for_database_authentication" do
it "finds user either by username or email" do
user = Factory(:user)
User.find_for_database_authentication(:login => user.username).should == user
User.find_for_database_authentication(:login => user.email).should == user
end
end

describe "#can_delete?" do
before(:each) do
@user = Factory(:refinery_user)
@user_not_persisted = Factory.build(:refinery_user)
@super_user = Factory(:refinery_user)
@super_user.add_role(:superuser)
let(:user_not_persisted) { Factory.build(:refinery_user) }
let(:super_user) do
super_user = Factory(:refinery_user)
super_user.add_role(:superuser)
super_user
end

context "won't allow to delete" do
it "not persisted user record" do
@user.can_delete?(@user_not_persisted).should be_false
refinery_user.can_delete?(user_not_persisted).should be_false
end

it "user with superuser role" do
@user.can_delete?(@super_user).should be_false
refinery_user.can_delete?(super_user).should be_false
end

it "if user count with refinery role <= 1" do
Role[:refinery].users.delete(@user)
@super_user.can_delete?(@user).should be_false
Role[:refinery].users.delete(refinery_user)
super_user.can_delete?(refinery_user).should be_false
end

it "user himself" do
@user.can_delete?(@user).should be_false
refinery_user.can_delete?(refinery_user).should be_false
end
end

context "allow to delete" do
it "if all conditions return true" do
@super_user.can_delete?(@user).should be_true
super_user.can_delete?(refinery_user).should be_true
end
end
end

describe "#plugins=" do
it "assigns plugins to user" do
user = Factory(:user)
plugin_list = ["refinery_one", "refinery_two", "refinery_three"]
user.plugins = plugin_list
user.plugins.collect { |p| p.name }.should == plugin_list
Expand All @@ -126,7 +123,6 @@

describe "#authorized_plugins" do
it "returns array of user and always allowd plugins" do
user = Factory(:user)
["refinery_one", "refinery_two", "refinery_three"].each_with_index do |name, index|
user.plugins.create!(:name => name, :position => index)
end
Expand All @@ -135,25 +131,23 @@
end

describe "plugins association" do
before(:each) do
@user = Factory(:user)
@plugin_list = ["refinery_one", "refinery_two", "refinery_three"]
@user.plugins = @plugin_list
end
let(:plugin_list) { ["refinery_one", "refinery_two", "refinery_three"] }
before { user.plugins = plugin_list }

it "have a plugins attribute" do
@user.should respond_to(:plugins)
user.should respond_to(:plugins)
end

it "returns plugins in ASC order" do
@user.plugins[0].name.should == @plugin_list[0]
@user.plugins[1].name.should == @plugin_list[1]
@user.plugins[2].name.should == @plugin_list[2]
user.plugins[0].name.should == plugin_list[0]
user.plugins[1].name.should == plugin_list[1]
user.plugins[2].name.should == plugin_list[2]
end

it "deletes associated plugins" do
@user.destroy
UserPlugin.find_by_user_id(@user.id).should be_nil
user.destroy
UserPlugin.find_by_user_id(user.id).should be_nil
end
end

end
40 changes: 10 additions & 30 deletions images/spec/models/image_spec.rb
@@ -1,63 +1,43 @@
require 'spec_helper'

describe Image do

def reset_image(options = {})
@valid_attributes = {
:id => 1,
:image => File.new(File.expand_path('../../uploads/beach.jpeg', __FILE__))
}.merge(options)

@image.destroy if @image
@image = Image.create!(@valid_attributes)
end

def image_can_be_destroyed
@image.destroy.should == true
end

before(:each) do
reset_image
end

# clean up after ourselves.
after(:each) do
Image.destroy_all
let(:image) do
Image.create!(:id => 1,
:image => File.new(File.expand_path('../../uploads/beach.jpeg', __FILE__)))
end

context "with valid attributes" do
it "should create successfully" do
@image.errors.empty?
image.errors.should be_empty
end
end

context "image url" do
it "should respond to .thumbnail" do
@image.respond_to?(:thumbnail).should == true
image.should respond_to(:thumbnail)
end

it "should contain its filename at the end" do
@image.thumbnail(nil).url.split('/').last.should == @image.image_name
image.thumbnail(nil).url.split('/').last.should == image.image_name
end

it "should be different when supplying geometry" do
@image.thumbnail(nil).url.should_not == @image.thumbnail('200x200').url
image.thumbnail(nil).url.should_not == image.thumbnail('200x200').url
end

it "should have different urls for each geometry string" do
@image.thumbnail('200x200').url.should_not == @image.thumbnail('200x201').url
image.thumbnail('200x200').url.should_not == image.thumbnail('200x201').url
end

it "should use right geometry when given a thumbnail name" do
name, geometry = Image.user_image_sizes.first
@image.thumbnail(name).url.should == @image.thumbnail(geometry).url
image.thumbnail(name).url.should == image.thumbnail(geometry).url
end

end

describe "#title" do
it "returns a titleized version of the filename" do
@image.title.should == "Beach"
image.title.should == "Beach"
end
end

Expand Down

0 comments on commit fcdb18a

Please sign in to comment.