Ruby/calabash: undefined method `each' for 2:Fixnum (NoMethodError) - ruby

I'm stuck with an error while creating some auto test in calabash. So my code is:
Then /^I set some site$/ do
arr=["Google.com","Youtube.com"]
for i in arr.length {
touch("* id:'browserActivity_linLout_toolbar_url'")
sleep 5
currentSite=arr[i]
keyboard_enter_text (currentSite)
sleep 10
press_enter_button
i=i+1
sleep 20
}
end
end
When I try to run my test I get this error:
undefined method each' for 2:Fixnum (NoMethodError)
./features/step_definitions/calabash_steps.rb:339:in/^I set some site$/'
features/my_first.feature:6:in `Then I set some site'
Any ideas how to solve it?

You are writing the for loop with the wrong syntax. In Ruby, use:
for element in arr
#...
currentSite = element
#...
end
Or better, without for:
arr.each do |element|
#...
currentSite = element
#...
end

Related

undefined method 'execute' for nil:NilClass

I am making a tool in ruby which can interact with databases.
I am using amalgalite as an adapter for sqlite3.
Code:
require 'amalgalite'
# this is class RQuery
class RQuery
def db_open(db_name)
#db = Amalgalite::Database.new "#{db_name}.db"
make_class
end
def exec_this(query)
#db.execute(query)
end
def make_class
tables_list = exec_this("select name from sqlite_master where type='table'")
tables_list.each do |table|
#class_created = Object.const_set(table[0].capitalize, Class.new)
#class_created.class_eval do
define_singleton_method :first do
RQuery.new.exec_this("select * from #{table[0]} order by #{table[0]}.id ASC limit 1")
end
end
end
end
def eval_this(input)
instance_eval(input)
end
def code
print '>>'
input = gets
exit if input =~ /^q$/
puts eval_this(input)
code
end
end
Now when I am running the code everything works fine until I call table_name.first
It gives output
vbhv#fsociety ~/git/R-Query/bin $ ruby main.rb
Enter the code or q for quit
>>db_open('vbhv')
users
persons
people
programmers
>>Users.first
/home/vbhv/git/R-Query/lib/r-query.rb:36:in `instance_eval': undefined method `execute' for nil:NilClass (NoMethodError)
Did you mean? exec
from /home/vbhv/git/R-Query/lib/r-query.rb:29:in `block (3 levels) in make_class'
from (eval):1:in `eval_this'
from /home/vbhv/git/R-Query/lib/r-query.rb:36:in `instance_eval'
from /home/vbhv/git/R-Query/lib/r-query.rb:36:in `eval_this'
from /home/vbhv/git/R-Query/lib/r-query.rb:43:in `code'
from /home/vbhv/git/R-Query/lib/r-query.rb:44:in `code'
from /home/vbhv/git/R-Query/lib/r-query.rb:44:in `code'
from main.rb:4:in `<main>'
Now the 'execute' function it is talking about is inside amalgalite. What am I doing wrong here?? Thanks in Advance!
The problem in this was that the new class formed dynamically doesn't know about the connection variable '#db'. Hence the code solves the problem.
#class_created.instance_variable_set(:#database, #db)
A big thanks to Jagdeep Singh.

Rspec not finding class methods

I'm writing some tests for my backend jobs and I'm having a weird issue with rspec not finding my methods.
I wrote a simple class & test to illustrate the issue :
app/interactors/tmp_test.rb :
class TmpTest
def call
a = 10
b = 5
b.substract_two
return a + b
end
def substract_two
c = self - 2
return c
end
end
spec/interactors/tmp_test.rb :
require 'rails_helper'
describe TmpTest do
context 'when doing the substraction' do
it 'return the correct number' do
expect(described_class.call).to eq(13)
end
end
end
output:
TmpTest
when doing the substraction
return the correct number (FAILED - 1)
Failures:
1) TmpTest when doing the substraction return the correct number
Failure/Error: expect(described_class.call).to eq(13)
NoMethodError:
undefined method `call' for TmpTest:Class
# ./spec/interactors/tmp_test.rb:6:in `block (3 levels) in <top (required)>'
Finished in 0.00177 seconds (files took 1.93 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/interactors/tmp_test.rb:5 # TmpTest when doing the substraction return the correct number
It's not a class method, it's an instance method. Your test should look like this:
describe TmpTest do
subject(:instance) { described_class.new }
context 'when doing the subtraction' do
it 'returns the correct number' do
expect(instance.call).to eq(13)
end
end
end
This is a complete mess. Corrected version with comments:
class TmpTest
def call
a = 10
b = 5
# b.substract_two # why do you call method of this class on b?!
a + subtract_two(b)
end
def substract_two(from)
from - 2
end
end
Also: don’t use return in the very last line of the method.

Instance variable showing up as `nil` in rspec

I have the following test code:
require_relative '../spec_helper'
describe Chess::King do
before do
#piece = Chess::King.new('king1',:black)
#board = Chess::Board.new
end
describe '#possible_moves' do
context "placing king at location 4,5" do
#board.grid[4][5] = #piece
subject {#piece.possible_moves(#board)}
it {is_expected.to eq([3,5],[3,6],[4,6],[5,6],[5,5])}
end
end
end
Why am I getting this error:
in block (3 levels) in <top (required)>': undefined methodgrid' for nil:NilClass (NoMethodError)
I am not sure about this line: #board.grid[4][5] = #piece.
My intention here is to assign the piece object to the grid instance variable of board (8x8 array).
Try using let instead:
require_relative '../spec_helper'
describe Chess::King do
let(:piece) { Chess::King.new('king1',:black) }
let(:board) { Chess::Board.new }
describe '#possible_moves' do
context "placing king at location 4,5" do
before(:each) { board.grid[4][5] = piece }
subject {piece.possible_moves(board)}
it {is_expected.to eq([3,5],[3,6],[4,6],[5,6],[5,5])}
end
end
end

Rspec double returning NoMethodError

Not sure if my syntax is right, it doesn't seem to recognise the stub I've passed to my double.
class Robot
def boogie friend
friend.dances
end
end
Test:
describe Robot do
let(:robo_friend){double(:friend, {dances: true})}
it "should have a friend dance too" do
expect(subject.boogie :robo_friend).to be true
end
end
And the error:
Robot should have a friend dance too
Failure/Error: expect(subject.boogie :robo_friend).to be true
NoMethodError:
undefined method `dances' for :robo_friend:Symbol
# ./lib/robot.rb:3:in `boogie'
# ./spec/robot_spec.rb:8:in `block (2 levels) in <top (required)>'
Can anyone see what I'm doing wrong?
This will work, you need to pass the object and not a symbol.
describe Robot do
let(:robo_friend) { double("Friend", dances: true) }
it "should have a friend dance too" do
expect(subject.boogie(robo_friend).to eq(true)
end
end

Why do I get "Undefined method ::new" in simple classes?

I am writing an ATM-system-like socket/server solution. I would appreciate if someone could tell me what I'm missing. For some reason, I get the following error running my stub test suite:
# Running tests:
.E
Finished tests in 0.002411s, 829.4384 tests/s, 414.7192 assertions/s.
1) Error:
test_0001_connects_to_a_host_with_a_socket(AtmClient::connection):
NoMethodError: undefined method `new' for #<SpoofServer:0x9dce2dc #clients=[], #server=#<TCPServer:fd 5>>
/media/wildfyre/Files/Programming/KTH/progp/Atm/spec/client/SpoofServer.rb:12:in `start'
/media/wildfyre/Files/Programming/KTH/progp/Atm/spec/client/client_spec.rb:12:in `block (3 levels) in <top (required)>'
2 tests, 1 assertions, 0 failures, 1 errors, 0 skips
My minispec file is:
require_relative '../spec_helper.rb'
require_relative '../../lib/AtmClient.rb'
require_relative 'SpoofServer.rb'
describe AtmClient do
it "can be created with no arguments" do
AtmClient.new.must_be_instance_of AtmClient
end
describe 'connection' do
it "connects to a host with a socket" do
spoof = SpoofServer.new.start
client = AtmClient.new.connect
spoof.any_incoming_connection?.must_be true
spoof.kill
end
end
end
My SpoofServer file is:
require 'socket'
class SpoofServer
def initialize
end
def start
#clients = []
#server = TCPServer.new 1234
#listener_thread = new Thread do
#clients.add #server.accept
end
end
def any_incoming_connection?
#clients.size > 0
end
def kill
#listener_thread.exit
#clients.each {|c| c.close}
end
end
As you can read in the trace of the calls stack:
NoMethodError: undefined method `new' for #<SpoofServer:...>
/.../spec/client/SpoofServer.rb:12:in `start'
The error is inside the start method defined in SpoofServer.rb, at line 12, the wrong line is:
#listener_thread = new Thread do
That should be:
#listener_thread = Thread.new do
As you have written it, what you are actually doing is to calling the new method passing the Thread class as argument. Since no new method is defined for instances of the SpoofServer class you get the NoMethodError exception.
In body of instance method SpoofServer#start, you can't call the class method SpoofServer.new by new.

Resources