Couverture de tests presque à 100%

This commit is contained in:
echarp 2014-09-03 02:14:21 +02:00
parent 7da30d42a5
commit 8f1d374eb5
9 changed files with 103 additions and 24 deletions

View File

@ -5,9 +5,7 @@ class MapsController < ApplicationController
def index
respond_to do |format|
format.html
format.json do
render json: Event.moderated.future.geo.map { |event| event.to_json }
end
format.json { render json: Event.moderated.future.geo }
end
end
end

View File

@ -69,17 +69,15 @@ class Event < ActiveRecord::Base
end
end
def to_json
def as_json(_options = {})
{ type: 'Feature', properties: {
name: title,
popupContent: "<a href=\"/#{self.class.name.downcase.pluralize}/#{id}\"" \
+ ">#{city}: #{title}</a>"
},
geometry: {
}, geometry: {
type: 'Point',
coordinates: [longitude, latitude]
}
}
} }
end
def full_address

View File

@ -3,6 +3,8 @@ class Note < ActiveRecord::Base
belongs_to :event
belongs_to :author, class_name: User
validates :contents, presence: true
# Setup the magic time stamp so it uses the "date" column
def timestamp_attributes_for_create
super << :date

View File

@ -10,6 +10,8 @@ class User < ActiveRecord::Base
has_many :notes
validates :login, presence: true
def encrypted_password=(pass)
self[:password] = pass
end

View File

@ -57,6 +57,18 @@ class EventsControllerTest < ActionController::TestCase
assert_redirected_to root_url
end
test 'should not create event' do
assert_no_difference 'Event.count' do
post :create, event: {
title: @event.title,
city: @event.city,
region: @event.related_region
}
assert_not_empty assigns(:event).errors.messages
end
end
test 'should show event' do
get :show, id: @event
assert_response :success
@ -93,6 +105,14 @@ class EventsControllerTest < ActionController::TestCase
assert_redirected_to :root
end
test 'should not update event' do
patch :update, id: @event, secret: 'MyString', event: {
title: nil
}
assert_not_empty assigns(:event).errors.messages
end
test 'should get cancel page' do
get :cancel, id: @event, secret: 'MyString'
assert_response :success

View File

@ -11,6 +11,12 @@ class ModerationsControllerTest < ActionController::TestCase
sign_in users(:one)
end
test 'should get index' do
get :index
assert_response :success
assert_not_nil assigns(:events)
end
test 'should preview event' do
patch :preview, id: @moderation, event: {
title: 'hello world',
@ -26,6 +32,13 @@ class ModerationsControllerTest < ActionController::TestCase
assert_redirected_to moderations_path
end
test 'should not edit event' do
put :update, id: @moderation, event: {
title: nil
}
assert_not_empty assigns(:moderation).errors
end
test 'should accept event' do
@moderation = events :proposed
@ -50,21 +63,23 @@ class ModerationsControllerTest < ActionController::TestCase
start_time: @moderation.start_time,
end_time: @moderation.end_time,
description: @moderation.description,
city: @moderation.city,
region: @moderation.related_region,
locality: @moderation.locality,
url: @moderation.url,
contact: @moderation.contact,
moderated: @moderation.moderated,
secret: @moderation.secret,
submission_time: @moderation.submission_time,
submitter: @moderation.submitter,
tags: @moderation.tags }
tags: @moderation.tags
}
assert_empty assigns(:moderation).errors
assert_redirected_to moderations_path
end
test 'should not update event' do
patch :update, id: @moderation, secret: 'MyString', event: {
title: nil
}
assert_not_empty assigns(:moderation).errors
end
test 'should reject event' do
assert_difference 'Event.count', -1 do
delete :destroy, id: @moderation
@ -74,4 +89,14 @@ class ModerationsControllerTest < ActionController::TestCase
assert_redirected_to moderations_path
end
test 'should reject event with a reason' do
assert_difference 'Event.count', -1 do
delete :destroy, id: @moderation, reason: 'r_4'
end
assert_empty assigns(:moderation).errors
assert_redirected_to moderations_path
end
end

View File

@ -18,12 +18,30 @@ class NotesControllerTest < ActionController::TestCase
test 'should create note' do
assert_difference('Note.count') do
post :create, moderation_id: @note.event.id, note: {
author: @note.author,
contents: @note.contents,
date: @note.date
contents: @note.contents
}
end
assert_redirected_to moderations_path
end
test 'should send mail' do
assert_difference('Note.count') do
post :create, moderation_id: @note.event.id, envoiParMail: 'oui', note: {
contents: @note.contents
}
end
assert ActionMailer::Base.deliveries.present?
assert_redirected_to moderations_path
end
test 'should not create note' do
assert_no_difference('Note.count') do
post :create, moderation_id: @note.event.id, note: {
nothing: 'almost'
}
end
end
end

View File

@ -35,6 +35,14 @@ class UsersControllerTest < ActionController::TestCase
assert_redirected_to user_path(assigns(:user))
end
test 'should not create user' do
assert_no_difference('User.count') do
post :create, user: {
login: nil
}
end
end
test 'should show user' do
get :show, id: @user
assert_response :success
@ -55,6 +63,14 @@ class UsersControllerTest < ActionController::TestCase
assert_redirected_to user_path(assigns(:user))
end
test 'should not update user' do
patch :update, id: @user, user: {
login: nil
}
assert_not_empty assigns(:user).errors
end
test 'should destroy user' do
assert_difference('User.count', -1) do
delete :destroy, id: @user

View File

@ -113,10 +113,10 @@ class EventTest < ActiveSupport::TestCase
end
test 'json transform' do
assert_not_nil @event.to_json
assert_not_nil @event.as_json
assert_equal 'Feature', @event.to_json[:type]
assert_equal 'Point', @event.to_json[:geometry][:type]
assert_equal 'Feature', @event.as_json[:type]
assert_equal 'Point', @event.as_json[:geometry][:type]
end
test 'full address' do