How to create functional tests for a Redmine 3 plugin - redmine

I have created few Redmine 3 plugins and now I want to code the tests to ensure a better stability if possible.
But now I can't create anything that works, I tried to look at other plugin's tests, and I can't reproduce things to work in any way possible.
You guys can give me a simple canvas example of how to proceed ?
I actually created redmine/plugins/redmine_timebank/test/functional/timebank_helper_test.rb
containing this :
require 'redmine'
require 'versions_controller'
class TimebankHelperTest < ActionController::TestCase
fixtures :projects,
:issues,
:issue_statuses,
:versions,
:trackers,
:projects_trackers,
:issue_categories,
:time_entries
def setup
#controller = VersionsController.new
#request = ActionController::TestRequest.new
#response = ActionController::TestResponse.new
User.current = User.where(:admin => true).first
#request.session[:user_id] = User.current.id
#project = Project.find(1)
end
def test_timebank_table
puts "AWWWWWWWWWWWWWWWWWWWW YEAHHHHHHHHHHHHHHHHHHHHH !"
end
end
But when I execute the command :
RAILS_ENV=test bin/rake redmine:plugins:test --trace
I get those errors :
** Execute redmine:plugins:test:functionals
/usr/bin/ruby2.3 -I"lib:test" "/usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb" "plugins/*/test/functional/**/*_test.rb"
/usr/share/redmine/lib/redmine/core_ext/active_record.rb:18:in `<top (required)>': uninitialized constant ActiveModel (NameError)
from /usr/share/redmine/lib/redmine/core_ext.rb:1:in `require'
from /usr/share/redmine/lib/redmine/core_ext.rb:1:in `block in <top (required)>'
from /usr/share/redmine/lib/redmine/core_ext.rb:1:in `each'
from /usr/share/redmine/lib/redmine/core_ext.rb:1:in `<top (required)>'
from /usr/share/redmine/lib/redmine.rb:18:in `require'
from /usr/share/redmine/lib/redmine.rb:18:in `<top (required)>'
from /usr/share/redmine/plugins/redmine_timebank/test/functional/timebank_helper_test.rb:1:in `require'
from /usr/share/redmine/plugins/redmine_timebank/test/functional/timebank_helper_test.rb:1:in `<top (required)>'
from /usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb:10:in `require'
from /usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb:10:in `block (2 levels) in <main>'
from /usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb:9:in `each'
from /usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb:9:in `block in <main>'
from /usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb:4:in `select'
from /usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb:4:in `<main>'
rake aborted!
Seeing uninitialized constant ActiveModel (NameError) make me thinks that I need more depedencies, but I tough that I just need to require redmine to all import the rest.
Other plugins seems to import some thing like in their test/test_helper.rb :
require File.expand_path(File.dirname(__FILE__) + '/../../../test/test_helper')
And this line into each testing file :
require File.dirname(__FILE__) + '/../test_helper'
Rake test says that none of those importing files could be found. Such as :
/usr/share/redmine/plugins/redmine_timebank/test/test_helper.rb:1:in `require': cannot load such file -- /usr/share/redmine/test/test_helper (LoadError)
Which stuff I do need to import into my functional test in order to get it works ?
Is this test/test_helper still exists ? Where can I find it ?
Thank you very much and have a great day !

I'll assume you have a plugin called redmine_foo that you want to test.
What you've seen other plugins do is correct, include Redmine's test/test_helper by doing
require File.expand_path(File.dirname(__FILE__) + '/../../../test/test_helper')
in redmine_foo/test/test_helper.rb.
If your Redmine does not have test/test_helper.rb, this is a sure sign something is wrong / missing. I'm not sure running tests against a Redmine located in /usr/share is a good idea anyway, so first of all, get a proper checkout of Redmine from github or the official SVN on redmine.org, add your plugin to the plugins directory (i.e. in a folder named plugins/redmine_foo) and work with that.
In your test case, require your plugin's test helper (the one-liner from above), i.e. in redmine_foo/test/functional/foos_controller_test.rb do:
require File.expand_path('../../test_helper', __FILE__)
class FoosControllerTest < ActionController::TestCase
setup do
# setup stuff specific to your application / test case
end
test 'should show hello world' do
get :show
assert_response :success
assert_match /hello world/, response.body
end
end
There is no need to initialize any of the #controller, #request, #response instance variables if your test case inherits from ActionController::TestCase.
To get the most simple Redmine plugin possible, with above test running successfully, all you have to do is to create the controller, add a route and basic init.rb:
redmine_foo/app/controllers/foos_controller:
class FoosController < ApplicationController
def show
render text: 'hello world'
end
end
redmine_foo/init.rb:
Redmine::Plugin.register :redmine_foo do
name 'Redmine Foo'
version '1.0.0'
end
redmine_foo/config/routes.rb:
resource :foo
Run your tests from the Redmine base directory using NAME=redmine_foo bin/rake redmine:plugins:test

Related

No such key :Enter after migration from Poltergeist to Selenium

Trying to get specs passing in an old Rails 4 project after migration from Poltergeist to Selenium WebDriver/ChromeDriver. Typical failure around .native.send_key(:Enter)
Is there an equivalent or best practice we should switch to?
17) Comment creation for image changes counter
Failure/Error: find('input[name="comment[body]"]').native.send_key(:Enter)
Selenium::WebDriver::Error::UnsupportedOperationError:
no such key :Enter
# ./spec/support/helpers/comments_page_helpers.rb:13:in `add_comment'
# ./spec/features/comments/creation_spec.rb:72:in `block (4 levels) in <top (required)>'
# ./spec/features/comments/creation_spec.rb:71:in `block (3 levels) in <top (required)>'
#spec/features/comments/creation_spec.rb
require 'spec_helper'
feature 'Comment creation', type: :feature, js: true do
...
context 'for image' do
background do
open_image_comments_modal section_position: 1, photo_position: 1
within '.modal-comments-container' do
add_comment 'First comment message'
end
end
...
end
#spec/support/helpers/comments_page_helpers.rb
module CommentsPageHelpers
...
def add_comment(text)
fill_in 'comment[body]', with: text
find('input[name="comment[body]"]').native.send_key(:Enter)
expect(page).to have_css '.comments .comment-body', text: text
end
...
end
If you need to send the enter key you shouldn't be calling anything on native and you should be using lowercase for the symbols, which would then work with Poltergeist or Selenium as the driver
find('input[name="comment[body]"]').send_keys(:enter)
See https://www.rubydoc.info/gems/capybara/Capybara/Node/Element#send_keys-instance_method

Rails `unitialized constant` for a constant defined in app/classes subdirectory in staging env

the error happens in :staging environment only
config/initializers/activity_api.rb:4:in 'block in <top (required)>'
Rails.application.config.to_prepare do
config = YAML.load_file('config/activity.yml')[Rails.env] || {}
config.deep_symbolize_keys!
Activity::API.config = config
end
And I have Activity::API class definition in app/classes/activity/api.rb
module Activity
class API
...
end
end
Shall I explicitely define a module Activity in app/classes/activity.rb and require files in app/classes/activity or there is something I misunderstand?
Maybe app/classes subdirectories are not in autoload path?
Creating an empty module Activity would help.
You can also try to user the inline class declaration style:
class Activity::Api
end

NameError: uninitialized constant ModelName

I'm trying to upload some data from a CSV into my db (sqlite3, in rails 4). I followed the steps listed here, not only the ones included in the first comment, but also the alternative option in the 2nd comment. I'm struggling with a NameError: uninitialized constant Drawing (my Model name).
I have tried to fix it by including config.autoload_paths += %W(#{config.root}/lib) as adviced here, but no luck. Any idea how to tackle this issue? Don't want to store the CSV file within the app, but exctract its rows and create records in my DB with their values.
lib/tasks/import_drawings_csv.rake
task :drawing => :environment do
require 'csv'
csv_text = File.read('path/to/public/imageshello.csv')
CSV.foreach("path/to/public/imageshello.csv", :headers => true) do |row|
Drawing.create!(row.to_hash)
end
end
terminal trace
javiers-mbp:colour malditojavi$ rake import_drawings_csv
rake aborted!
NameError: uninitialized constant Drawing
/Users/malditojavi/Desktop/colour/lib/tasks/import_drawings_csv.rake:5:in `block in <top (required)>'
/Users/malditojavi/Desktop/colour/lib/tasks/import_drawings_csv.rake:4:in `<top (required)>'
/Users/malditojavi/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/engine.rb:658:in `load'
/Users/malditojavi/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/engine.rb:658:in `block in run_tasks_blocks'
/Users/malditojavi/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/engine.rb:658:in `each'
/Users/malditojavi/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/engine.rb:658:in `run_tasks_blocks'
/Users/malditojavi/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/application.rb:438:in `run_tasks_blocks'
/Users/malditojavi/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/engine.rb:453:in `load_tasks'
/Users/malditojavi/Desktop/colour/Rakefile:6:in `<top (required)>'
/Users/malditojavi/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `eval'
/Users/malditojavi/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `<main>'
(See full trace by running task with --trace)
javiers-mbp:colour malditojavi$ rake import_drawings_csv
application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Colour
class Application < Rails::Application
# new line added for autoloading libs
config.autoload_paths += %W(#{config.root}/lib)
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
end
end
Using below syntax will fix your issue.
namespace :update_drawing do
desc "Drawing desc"
task :draw_me => :environment do
csv_text = File.read('path/to/public/imageshello.csv')
CSV.foreach('path/to/public/imageshello.csv', :headers => true) do |row|
Drawing.create!(row.to_hash)
end
end
end

Issues with Friendly_ID gem - possible issue with active admin

I ran the migrations as stated and added this to my model, but I keep generating the following error. Not really sure what is going on. My slug column in the photos table is full of unique values as well.
Im running rails 4.
Thanks for any comments in advance.
photo.rb
include FriendlyId
friendly_id :name, use: :slugged
error in console
NameError: uninitialized constant Photo::FriendlyId
from /Users/jhorsch/Repos/horschgallery2/app/models/photo.rb:4:in `<class:Photo>'
from /Users/jhorsch/Repos/horschgallery2/app/models/photo.rb:1:in `<top (required)>'
from /Users/jhorsch/Repos/horschgallery2/app/admin/photo.rb:1:in `<top (required)>'
from /Users/jhorsch/.rvm/gems/ruby-2.0.0-p247/bundler/gems/active_admin-ceb096c5ed4f/lib/active_admin/application.rb:179:in `load'

uninitialized constant FactoryGirl when run rspec, but works in console

This is kind of my first time using rspec. I am getting uninitialized constant FactoryGirl when running rspec. I tried in in the console by running, rails c test, and it recognizes FactoryGirl properly.
Here is the output from rspec:
Failures:
1) basic API gives authentication token from username and password
Failure/Error: user = FactoryGirl.create(:user, last_name: last_name)
NameError:
uninitialized constant FactoryGirl
# ./spec/requests/api/v1/api_spec.rb:4:in `block (2 levels) in <top (required)>'
Finished in 0.00069 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/requests/api/v1/api_spec.rb:2 # basic API gives authentication token from username and password
And this is my spec/spec_helper.rb:
# 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'
require 'factory_girl_rails'
FactoryGirl.find_definitions
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
end
I am not quite sure where to require 'factory_girl_rails and FactoryGirl.find_definitions because in the guide it just said "all you'll need to do is run FactoryGirl.find_definitions "
I forgot to require 'spec_helper' in my spec file.