Rspec, capybara getting started issues - ruby

I have have started with simple example of Rspec,Capybara. I have come across few issues. This issues are probably because I have experience with cucumber and page_object gem, but here I am using capybara and Site_prism gem.
I have tried:
my_example_spec.rb
require_relative 'Support/spec_helper'
require_relative 'pages/login_page'
describe 'My behaviour' do
it 'should do something'do
#login_page = LoginPage.new
#login_page.load
#login_page.login('autouser','password')
end
end
and login_page.rb
class LoginPage < SitePrism::Page
set_url "/login"
element :username, "input[id='username']"
element :password, "input[id='password']"
element :submit, "input[id='submit']"
def login(username,password)
#login_page.username.set username
#login_page.password.set password
#login_page.submit.click
end
end
Issues are:
When I run my_example_spec.rb it gives error
Testing started at ...
Run options: include {:full_description=>/My\ behaviour\ should\ do\ something/}
NoMethodError: undefined method `username' for nil:NilClass
./pages/login_page.rb:10:in `login'
./my_example_spec.rb:11:in `block (2 levels) in <top (required)>'
-e:1:in `load'
-e:1:in `<main>'
Shouldn't it be on(LoginPage).login (autouser, password). It should navigate to the page and run login method. It is how it works in page_object gem whats the equivalent of site_prism gem

The login method in your LoginPage class should be
def login(username,password)
username.set username
password.set password
submit.click
end
#login_page is not an instance variable of the LoginPage class so it is not accessible inside the class. It's also not necessary inside the class since you're already inside the class.

Related

Hanami: undefined method `size' for nil:NilClass

I keep learning hanami and ran into the following problem: when I initialize and check the parameters, I encounter the following error
Boot Error
Something went wrong while loading /Users/anewaccount/Projects/Mediateka/config.ru
NoMethodError: undefined method `size' for nil:NilClass
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/dry-validation-0.11.0/lib/dry/validation/schema/deprecated.rb:11:in `input_processor'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/dry-validation-0.11.0/lib/dry/validation/schema/class_interface.rb:165:in `default_options'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/dry-validation-0.11.0/lib/dry/validation/schema/class_interface.rb:35:in `new'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-validations-1.3.7/lib/hanami/validations.rb:109:in `validations'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-controller-1.3.3/lib/hanami/action/params.rb:152:in `params'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-controller-1.3.3/lib/hanami/action/validatable.rb:100:in `block in params'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-controller-1.3.3/lib/hanami/action/validatable.rb:100:in `class_eval'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-controller-1.3.3/lib/hanami/action/validatable.rb:100:in `params'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:11:in `<class:SignUp>'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:9:in `<module:Auth>'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:8:in `<module:Controllers>'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:7:in `<module:Web>'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:6:in `<top (required)>'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:56:in `require_relative'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:56:in `block in require!'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:94:in `each'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:94:in `for_each_file_in'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:56:in `require!'
my action:
# frozen_string_literal: true
require 'pry-byebug'
require_relative 'auth_base_action'
module Web
module Controllers
module Auth
class SignUp < AuthBaseAction
params do
required(:user).schema do
required(:first_name).filled(:str?)
required(:last_name).filled(:str?)
required(:email).filled(:str?, format?: /#/)
required(:password).filled(:str?).confirmation
end
end
def call params
binding.pry
end
end
end
end
end
I wrote the params block according to the example from the official documentation on the idea that it should work, but I catch NoMethodError: undefined method `size 'for nil: NilClass I also saw in the official documentation that these validations are delegated from Hanami :: Validations, I installed gem' hanami- validation 'according to their official documentation, but I had the same error, then I started looking at other people's code and they were successful using the example from the official documentation. Please tell me what is the error or please explain what am I doing wrong? I'm already very confused

Celluloid 0.17.3 giving unexpected "undefined method" error

I have started using Celluloid gem this morning for that first time. I am following this Railscasts tutorial and trying to figure things out.
I have a class called "SomeClass" and it has only one method. Here is the code:
require 'celluloid'
class SomeClass
include Celluloid
def initialize(name)
#name = name
end
def assholify()
puts "#{#name} has become an ASSHOLE."
end
end
When I create new instances of the class and call its method (with a bang i.e. "assholify!"), I am getting the undefined method 'assholify!', error. But Celluloid is supposed to trigger the method asynchronously when it is called with a bang. So here is how I am calling the method:
names = ['John', 'Tom', 'Harry']
names.each do |name|
n = SomeClass.new name
n.assholify!
end
Here is the full backtrace of the error:
I, [2016-09-09T11:28:02.488618 #3682] INFO -- : Celluloid 0.17.3 is running in BACKPORTED mode. [ http://git.io/vJf3J ]
/home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/calls.rb:42:in `rescue in check': undefined method `assholify!' for #<SomeClass:0x10897dc> (NoMethodError)
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/calls.rb:39:in `check'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/calls.rb:26:in `dispatch'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/call/sync.rb:16:in `dispatch'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/cell.rb:50:in `block in dispatch'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/cell.rb:76:in `block in task'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/actor.rb:339:in `block in task'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/task.rb:44:in `block in initialize'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/task/fibered.rb:14:in `block in create'
from (celluloid):0:in `remote procedure call'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/call/sync.rb:45:in `value'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/proxy/sync.rb:22:in `method_missing'
from some_class.rb:18:in `block in <main>'
from some_class.rb:16:in `each'
from some_class.rb:16:in `<main>'
Why am I getting this error? Is it the right way to call the function? Also how do I get rid of Celluloid 0.17.3 is running in BACKPORTED mode. warning?
The undefined method error occurred because actor methods are not called with a bang in the recent versions of celluloid gem. Instead you call the method like this: n.async.assholify. So here is what the code should look like:
names = ['John', 'Tom', 'Harry']
names.each do |name|
n = SomeClass.new name
n.async.assholify # Instead of "n.assholify!"
end
For "Celluloid 0.17.0 is running in BACKPORTED mode" warning, take a look at this wiki. Backported Mode is the default, for a limited time. If you use require 'celluloid/current' instead of require 'celluloid', you should not see this warning.

Rspec validation of method definition - Failure/Error

In Rspec, testing whether an instance is able to call method x.
DockingStation.rb
class DockingStation
def release_bike
end
end
Docking_spec.rb
require_relative '../lib/DockingStation'
describe DockingStation do
before(:each) do
#dockstat = DockingStation.new
end
describe "#DockingStation" do
it "Check release method" do
expect(#dockstat).to respond_to(:release_bike)
end
end
end
Currently getting the following error message:
1) DockingStation#DockingStation Check release method
Failure/Error: expect(#dockstat).to respond_to(:release_bike)
expected #<DockingStation:0x007fa518a6da00> to respond to :release_bike
# ./spec/Docking_spec.rb:10:in `block (3 levels) in <top (required)>'
What I'm expecting is for the object #dockstat instantiated in the Docking_spec.rb to respond to the release_bike method defined in DockingStation.rb, but this is not the case.
require_relative '../DockingStation'

Rspec and Rails for Zombies-Test not working

Im going through the rspec rails for zombies lessons and i am having trouble running the tests in that they are not reading my ruby code in my App/models folder. I even tried to put the ruby file i call zombie.rb into the spec folder itself and require_relative and still the test are failing can someone please help me out. I am a newbie and I find that TDD is the best and fastest way to learn to code profeciently.My code is below, What I have in both the zombie_spec.rb file as well as the zombie.rb file respecively:
require_relative 'spec_helper'
require_relative 'zombie'
describe Zombie do
it 'is invalid without a name' do
zombie = Zombie.new
zombie.should_not be_valid
end
it 'include tweets' do
tweet1 = Tweet.new(status: 'Uuuuunhhhhh')
tweet2 = Tweet.new(status: 'Arrrrggggg')
zombie = Zombie.new(name: 'Ash', tweets: [tweet1, tweet2])
zombie.tweets.should include(tweet1)
zombie.tweets.should include(tweet2)
end
end
and the zombie.rb file here
class Zombie < ActiveRecord::Base
attr_accessor :Tweet
validates :name, presence: true
end
This is the test result i'm getting
1) Zombie is invalid without a name
Failure/Error: zombie = Zombie.new
ActiveRecord::StatementInvalid:
Could not find table 'zombies'
# ./zombie_spec.rb:8:in `block (2 levels) in <top (required)>
2) Zombie include tweets
Failure/Error: tweet1 = Tweet.new(status: 'Uuuuunhhhhh')
NameError:
uninitialized constant Tweet
# ./zombie_spec.rb:15:in `block (2 levels) in <top (required)>'
It looks like your finding the Zombie model, because you're getting an error from ActiveRecord and it's complaining about the fact that it can't find the zombies table in your database, which suggests you haven't done your database migration yet (or had a problem with it).
The second error indicates that you haven't required any file that defines Tweet.

Sinatra, Mongoid, Heroku, MongoHQ: connecting to Mongodb

Trying to get Mongoid up and running with Sinatra on Heroku (MongoHQ). Previous experience with Rails but first time with the stack and Sinatra.
Started with one of the simple examples on the web (app.rb):
require 'rubygems'
require 'sinatra'
require 'mongo'
require 'mongoid'
configure do
Mongoid.load!('mongoid.yml')
Mongoid.configure do |config|
if ENV['MONGOHQ_URL']
conn = Mongo::Connection.from_uri(ENV['MONGOHQ_URL'])
uri = URI.parse(ENV['MONGOHQ_URL'])
# problem happens here
config.master = conn.db(uri.path.gsub(/^\//, ''))
else
config.master = Mongo::Connection.from_uri("mongodb://localhost:27017").db('test')
end
end
end
# Models
class Counter
include Mongoid::Document
field :count, :type => Integer
def self.increment
c = first || new({:count => 0})
c.inc(:count, 1)
c.save
c.count
end
end
# Controllers
get '/' do
"Hello visitor n" + Counter.increment.to_s
end
For reference, mongoid.yml looks like:
development:
sessions:
default:
database: localhost
production:
sessions:
default:
uri: <%= ENV['MONGOHQ_URL'] %>
As per app.rb (# problem happens here), my logs say:
/app/app.rb:15:in `block (2 levels) in <top (required)>': undefined method `master=' for Mongoid::Config:Module (NoMethodError)
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.3/lib/mongoid.rb:112:in `configure'
from /app/app.rb:11:in `block in <top (required)>'
from /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1273:in `configure'
from /app/app.rb:8:in `<top (required)>'
I have also tried variants, including:
config.master = Mongo::Connection.from_uri(ENV['MONGOHQ_URL']).db('appXXXXX')
Mongoid.database = Mongo::Connection.from_uri(ENV['MONGOHQ_URL']).db('appXXXXXXX')
But get the same error:
undefined method `master` for Mongoid::Config:Module (NoMethodError)
or:
undefined method `database=` for Mongoid::Config:Module (NoMethodError)
What am I missing?
Shouldn't be
configure do
Mongoid.load!('mongoid.yml')
end
enough?
That's what the mongid docs are saying. The MONGOHQ_URL environment variable already contains every information to initialize the connection to the db.
So was using Mongoid 3.x ... which:
Doesn't use 10gen driver: uses Moped
Doesn't use config.master
The canonical sample code above which is all over the web works out of the box with Mongoid 2.x so have dropped back to that for the time being.
Thanks!

Resources