Why am I getting uninitialized constant Calabash::ABase (NameError)? - calabash

I'm using Calabash. I ran calabash-android gen as described here. I have a step definition, a page object and a feature. Here's my page object:
class LocationScanPage < Calabash::ABase
LIST_BUTTON_QUERY="com.facebook.react.views.text.ReactTextView marked:'List'"
def trait
LIST_BUTTON_QUERY
end
def await(opts={})
wait_for_elements_exist([trait])
self
end
end
When I run bundle exec calabash-android run .\app-releaseStaging.apk I get:
uninitialized constant Calabash::ABase (NameError)

Adding require 'calabash-android' to the top of the page object fixed it:
require 'calabash-android'
class LocationScanPage < Calabash::ABase
LIST_BUTTON_QUERY="com.facebook.react.views.text.ReactTextView marked:'List'"
def trait
LIST_BUTTON_QUERY
end
def await(opts={})
wait_for_elements_exist([trait])
self
end
end
Calabash::ABase is defined here.

Related

How to extend ActiveJob to access delayed_job's failure/error hooks?

I'm using rails ActiveJob's delayed_job adaptor, but would like to still be able to access delayed_job's error and failure hooks to trigger notification mailers when a job errors or fails.
I think I've tracked down the piece of ActiveJob code I need to extend in order to incorporate delayed_job's error and failure methods.
How do I go about extending the JobWrapper class below to include delayed_job's error and failure methods, so I can then use custom error/failure code on a job-by-job basis?
require "delayed_job"
module ActiveJob
module QueueAdapters
class DelayedJobAdapter
def enqueue(job)
delayed_job = Delayed::Job.enqueue(JobWrapper.new(job.serialize), queue: job.queue_name, priority: job.priority)
job.provider_job_id = delayed_job.id
delayed_job
end
def enqueue_at(job, timestamp)
delayed_job = Delayed::Job.enqueue(JobWrapper.new(job.serialize), queue: job.queue_name, priority: job.priority, run_at: Time.at(timestamp))
job.provider_job_id = delayed_job.id
delayed_job
end
class JobWrapper
attr_accessor :job_data
def initialize(job_data)
#job_data = job_data
end
def perform
Base.execute(job_data)
end
end
end
end
end
I believe this is what you want
module ActiveJob
module QueueAdapters
class DelayedJobAdapter
class JobWrapper
def failure(job)
job_data = Base.deserialize(job.payload_object.job_data)
job_data.failure(job) if job_data.respond_to?(:failure)
end
end
end
end
end
I've posted this problem/patch to rails issue
https://github.com/rails/rails/issues/30205
ActiveJob implements ActiveSupport::Rescuable which means you can use rescue_from.
class MyJob < ActiveJob::Base
rescue_from Exception do |e|
# do something with e
end
# rest of your job
end

How to implement DelayedJob custom job for Prawn PDF rendering?

Im trying to use DelayedJob to render Prawn PDFs. Following the custom job code in the docs, I've come up with this:
/lib/jobs/pdf_handling.rb
RenderPdf = Struct.new( :id, :view_context ) do
def perform
user = User(id)
pdf = UserFolder.new( id, view_context )
name = "user_folder_report.pdf"
send_data pdf.render, filename: name, type: "application/pdf"
end
end
PagesController.rb
def user_folder
respond_to do |format|
format.pdf do
Delayed::Job.enqueue RenderPdf.new(#user, view_context)
end
end
end
this results in the error:
uninitialized constant PagesController::RenderPdf
Adding required RenderPdf at the top of the PagesController doesn't help.
What am I missing? How can I implement this so PDF generation occurs via DelayedJob? Thanks.
updates
When /jobs is moved under /apps the error changes to:
can't dump anonymous module: #<Module:0x007fca7a3ae638>
/application.rb
config.autoload_paths += Dir["#{config.root}/lib/assets/"]
updates
I changed
class RenderFolder < Struct.new( :type, :rating_id, :dis, :view_context )
def perform
to
class RenderFolder < ActiveJob::Base
def perform(...)
Then, using ActiveJob, you can do
RenderFolder.perform_later(...)
This seems to be working...Im still implementing.
the lib folder is no longer loaded by default in rails. you can either add it to the autoload_path or (what i would do) just have it in some app/xxx folder. typically, i have app/support or something for arbitrary utility classes.

how to set rails app globally available constant

I'm trying to define a global constant that I can access from anywhere in the application. For this I have created a file in initializer with this content:
APP_CONFIG = YAML.load_file(Rails.root + "config/app_config.yml").with_indifferent_access
And on a haml view I'm doing this
- if APP_CONFIG[:param]
%b HOLA MUNDO
but it says APP_CONFIG is not defined,
I tried using in both places:
##APP_CONFIG
#APP_CONFIG
$APP_CONFIG
Nothing works, I also tried with:
module MyApp
class Application
APP_CONFIG = .....
end
end
And from view:
MyApp::Application::APP_CONFIG[:param]
Also an error
uninitialized constant MyApp::Application::APP_CONFIG
You would be a lot better wrapping that up in a module. Something like this:
require 'yaml'
module AppConfig
class << self
def settings
#settings ||= hash.with_indifferent_access
end
def path
File.join(Rails.root, 'config' , 'app_config.yml')
end
def hash
YAML.load_file(path)
end
end
end
With that in place, you'll then be able to do this:
puts AppConfig.settings['foo']
With the Module you can gathering the data in a number of steps and easily test each step. It will also be easier to extend the behaviour.
And I'd put that in something like app/tools/app_config.rb

Minitest - test class in Rails 4

I am trying to use Minitest in a fresh Rails 4 install. My understanding is that if I have a class that doesn't inherit from ActiveRecord then I should be able to use Minitest itself, without Rails integration:
#test/models/blog.rb
require "minitest/autorun"
class Blog < Minitest::Unit::TestCase
def setup
#b = Blog.new
end
def test_entries
assert_empty "message", #b.entries
end
#app/models/blog.rb
class Blog
attr_reader :entries
def initialize
#entries = []
end
I run the test with ruby test/models/blog.rb.
My problem comes with the setup method. If I don't include an entry for my blog, the tests fails with the message that there are the wrong number of arguments in setup. If I include an entry in my setup message #b = Blog.new entries: "Twilight", my test fails in the test_entries method because entries is an undefined method.
You have a couple problems. First, you are not requiring "test_helper", which means that rails isn't getting loaded when you run this test, which means that the mechanism rails uses to resolve missing constants isn't loaded. You will either need to require the helper or require the blog file directly. Second, you are overwriting the constant you want to test with the test, which is why you are getting confusing messages. Name the test class BlogTest instead to avoid this.
This is what I think you are trying to do:
require "minitest/autorun"
require "models/blog" # Assuming "app" is in your load path when running the test
#require "test_helper" # Or require this instead if you need to use DB
class BlogTest < Minitest::Unit::TestCase
def setup
#b = Blog.new
end
def test_entries
assert_empty #b.entries, "Blog entries should be empty"
end
end

setting up controller for test

i'm trying to write a functional test for my controller in rubymotion using bacon.
in my controller i have some code that populates some data:
def viewWillAppear(animated)
song_text.text = chant.song_text
...
end
i am setting the chant variable when i push that controller to the navigation-controller.
this works fine in the app, but when i try to do that in a before-block of the spec it does not work, because viewWillAppear gets called before the block and it fails with a NoMethodError: undefined method 'song_text' for nil:NilClass.
is there some way to handle this situation? is there some other way to populate the data or use a different method than viewWillAppear?
I've experience a similar issue that was solved by calling the super method in the viewWillAppear method
The RubyMotion support answered my support ticket indicating, that this is the expected behavior of controllers in bacon.
I worked around the whole thing by stubbing my class under test in the bacon spec like so:
# spec/chant_controller_spec.rb
class ChantControllerStub < ChantController
def chant
#chant ||= create_chant
end
end
describe "ChantController" do
tests ChantControllerStub
it "displays a chant" do
view('verse').should.not == nil
end
end