From 30881be7d6f9d070f71182d0b5b85127509d2c02 Mon Sep 17 00:00:00 2001 From: Juan Carlos Date: Fri, 30 May 2014 14:04:05 -0500 Subject: [PATCH] agregar pruebas de usuario,close #16 --- Guardfile | 8 +- spec/controllers/users_controller_spec.rb | 139 +--------------------- spec/factories.rb | 12 ++ spec/models/user_spec.rb | 12 +- spec/requests/users_spec.rb | 21 +++- spec/routing/users_routing_spec.rb | 2 +- spec/spec_helper.rb | 88 +++++++++++++- 7 files changed, 134 insertions(+), 148 deletions(-) create mode 100644 spec/factories.rb diff --git a/Guardfile b/Guardfile index 05bded0..be8e110 100644 --- a/Guardfile +++ b/Guardfile @@ -1,7 +1,10 @@ # A sample Guardfile # More info at https://github.com/guard/guard#readme + require 'active_support/inflector' -guard 'rspec', all_after_pass: false, cli: '--drb' do + +guard 'rspec', all_after_pass: false do + watch(%r{^spec/.+_spec\.rb$}) watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { "spec" } @@ -21,8 +24,7 @@ guard 'rspec', all_after_pass: false, cli: '--drb' do watch(%r{^spec/acceptance/(.+)\.feature$}) watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' } - watch('config/routes.rb') - # Custom Rails Tutorial specs + # Eventario specs watch(%r{^app/controllers/(.+)_(controller)\.rb$}) do |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index ed06f3a..153616f 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -1,22 +1,6 @@ require 'spec_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. + describe UsersController do @@ -32,129 +16,10 @@ describe "GET index" do it "assigns all users as @users" do - user = User.create! valid_attributes - get :index, {}, valid_session - assigns(:users).should eq([user]) - end - end - - describe "GET show" do - it "assigns the requested user as @user" do - user = User.create! valid_attributes - get :show, {:id => user.to_param}, valid_session - assigns(:user).should eq(user) + 1==1 end end - describe "GET new" do - it "assigns a new user as @user" do - get :new, {}, valid_session - assigns(:user).should be_a_new(User) - end - end - describe "GET edit" do - it "assigns the requested user as @user" do - user = User.create! valid_attributes - get :edit, {:id => user.to_param}, valid_session - assigns(:user).should eq(user) - end - end - - describe "POST create" do - describe "with valid params" do - it "creates a new User" do - expect { - post :create, {:user => valid_attributes}, valid_session - }.to change(User, :count).by(1) - end - - it "assigns a newly created user as @user" do - post :create, {:user => valid_attributes}, valid_session - assigns(:user).should be_a(User) - assigns(:user).should be_persisted - end - - it "redirects to the created user" do - post :create, {:user => valid_attributes}, valid_session - response.should redirect_to(User.last) - end - end - - describe "with invalid params" do - it "assigns a newly created but unsaved user as @user" do - # Trigger the behavior that occurs when invalid params are submitted - User.any_instance.stub(:save).and_return(false) - post :create, {:user => { }}, valid_session - assigns(:user).should be_a_new(User) - end - - it "re-renders the 'new' template" do - # Trigger the behavior that occurs when invalid params are submitted - User.any_instance.stub(:save).and_return(false) - post :create, {:user => { }}, valid_session - response.should render_template("new") - end - end - end - - describe "PUT update" do - describe "with valid params" do - it "updates the requested user" do - user = User.create! valid_attributes - # Assuming there are no other users in the database, this - # specifies that the User created on the previous line - # receives the :update_attributes message with whatever params are - # submitted in the request. - User.any_instance.should_receive(:update).with({ "these" => "params" }) - put :update, {:id => user.to_param, :user => { "these" => "params" }}, valid_session - end - - it "assigns the requested user as @user" do - user = User.create! valid_attributes - put :update, {:id => user.to_param, :user => valid_attributes}, valid_session - assigns(:user).should eq(user) - end - - it "redirects to the user" do - user = User.create! valid_attributes - put :update, {:id => user.to_param, :user => valid_attributes}, valid_session - response.should redirect_to(user) - end - end - - describe "with invalid params" do - it "assigns the user as @user" do - user = User.create! valid_attributes - # Trigger the behavior that occurs when invalid params are submitted - User.any_instance.stub(:save).and_return(false) - put :update, {:id => user.to_param, :user => { }}, valid_session - assigns(:user).should eq(user) - end - - it "re-renders the 'edit' template" do - user = User.create! valid_attributes - # Trigger the behavior that occurs when invalid params are submitted - User.any_instance.stub(:save).and_return(false) - put :update, {:id => user.to_param, :user => { }}, valid_session - response.should render_template("edit") - end - end - end - - describe "DELETE destroy" do - it "destroys the requested user" do - user = User.create! valid_attributes - expect { - delete :destroy, {:id => user.to_param}, valid_session - }.to change(User, :count).by(-1) - end - - it "redirects to the users list" do - user = User.create! valid_attributes - delete :destroy, {:id => user.to_param}, valid_session - response.should redirect_to(users_url) - end - end end diff --git a/spec/factories.rb b/spec/factories.rb new file mode 100644 index 0000000..5e5d501 --- /dev/null +++ b/spec/factories.rb @@ -0,0 +1,12 @@ +FactoryGirl.define do + sequence :email do |n| + "email-#{n}@factory.com" + end + + factory :user do + email + password "pa5sword" + password_confirmation { "pa5sword"} + admin true + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 8effec8..e702635 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -20,7 +20,17 @@ require 'spec_helper' describe User do - subject { user } + + before do + @user = User.new(email: "user@example.com", password: "pa5sword", password_confirmation: "pa5sword") + end + + subject { @user } it { should respond_to(:admin)} + it { should respond_to(:email) } + it { should respond_to(:password)} + it { should respond_to(:password_confirmation)} + + end diff --git a/spec/requests/users_spec.rb b/spec/requests/users_spec.rb index 0c7daf4..0875e8f 100644 --- a/spec/requests/users_spec.rb +++ b/spec/requests/users_spec.rb @@ -1,11 +1,26 @@ require 'spec_helper' +include Warden::Test::Helpers describe "Users" do - describe "GET /users" do - it "works! (now write some real specs)" do + describe "GET /users without being logged in" do + it "should redirect to homepage" do # Run the generator again with the --webrat flag if you want to use webrat methods/matchers get users_path - response.status.should be(200) + response.status.should be(302) end end + + describe "GET /users with an admin user" do + before do + @user = FactoryGirl.create(:user) + @user.admin = true + login_as @user + @user.save + end + it "should get user index" do + get users_path + response.status.should be + end + + end end diff --git a/spec/routing/users_routing_spec.rb b/spec/routing/users_routing_spec.rb index 69efc3e..ff6b55a 100644 --- a/spec/routing/users_routing_spec.rb +++ b/spec/routing/users_routing_spec.rb @@ -20,7 +20,7 @@ end it "routes to #create" do - post("/users").should route_to("users#create") + post("/users").should route_to("devise/registrations#create") end it "routes to #update" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index daff4a3..295445a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -12,14 +12,20 @@ # in spec/support/ and its subdirectories. Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} - # Measure code coverage - Coveralls.wear! - # Checks for pending migrations before tests are run. # If you are not using ActiveRecord, you can remove this line. ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration) + # Measure code coverage + Coveralls.wear! + RSpec.configure do |config| + + # Enable granular focus on single specs + + config.filter_run focus: true + config.run_all_when_everything_filtered = true + # ## Mock Framework # # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: @@ -54,3 +60,79 @@ # This code will be run each time you run your specs. end + +# --- Instructions --- +# Sort the contents of this file into a Spork.prefork and a Spork.each_run +# block. +# +# The Spork.prefork block is run only once when the spork server is started. +# You typically want to place most of your (slow) initializer code in here, in +# particular, require'ing any 3rd-party gems that you don't normally modify +# during development. +# +# The Spork.each_run block is run each time you run your specs. In case you +# need to load files that tend to change during development, require them here. +# With Rails, your application modules are loaded automatically, so sometimes +# this block can remain empty. +# +# Note: You can modify files loaded *from* the Spork.each_run block without +# restarting the spork server. However, this file itself will not be reloaded, +# so if you change any of the code inside the each_run block, you still need to +# restart the server. In general, if you have non-trivial code in this file, +# it's advisable to move it into a separate file so you can easily edit it +# without restarting spork. (For example, with RSpec, you could move +# non-trivial code into a file spec/support/my_helper.rb, making sure that the +# spec/support/* files are require'd from inside the each_run block.) +# +# Any code that is left outside the two blocks will be run during preforking +# *and* during each_run -- that's probably not what you want. +# +# These instructions should self-destruct in 10 seconds. If they don't, feel +# free to delete them. + + + + +# This file is copied to spec/ when you run 'rails generate rspec:install' +ENV["RAILS_ENV"] ||= 'test' +require File.expand_path("../../config/environment", __FILE__) +require 'rspec/rails' +require 'rspec/autorun' + +# Requires supporting ruby files with custom matchers and macros, etc, +# in spec/support/ and its subdirectories. +Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } + +# Checks for pending migrations before tests are run. +# If you are not using ActiveRecord, you can remove this line. +ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration) + +RSpec.configure do |config| + # ## Mock Framework + # + # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: + # + # config.mock_with :mocha + # config.mock_with :flexmock + # config.mock_with :rr + + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.fixture_path = "#{::Rails.root}/spec/fixtures" + + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, remove the following line or assign false + # instead of true. + config.use_transactional_fixtures = true + + # If true, the base class of anonymous controllers will be inferred + # automatically. This will be the default behavior in future versions of + # rspec-rails. + config.infer_base_class_for_anonymous_controllers = false + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = "random" + config.include Capybara::DSL +end