I am trying to run given scenarios as many times per each account type. I found out in other thread that "around hooks" could be the best option to do it the DRY way.
Below is my code:
require 'watir-webdriver'
require 'page-object/page_factory'
require 'page_navigation'
require 'data_magic'
require_relative 'pages/login_page'
Before do
beforeCodeBlock
...
end
Around('#hooks_test') do|scenario, block|
DataMagic.load('default.yml')
account_type = {"listener" => DataMagic.yml["listener_data"],
"free" => DataMagic.yml["free_data"],
"premium" => DataMagic.yml["premium_data"]}
account_type .each {|key, value|
puts "Running scenario: #{scenario.name} as #{key} user"
visit_page(LoginPage)
on_page(LoginPage).sign_in(value["username"],value["password"]) #Login as
block.call
}
end
After do
afterCodeBlock
...
end
So when I run the feature file I get the following output and subsequent error:
Running scenario: Hooks test as listener user
undefined method `visit_page' for nil:NilClass (NoMethodError)
/myProject/features/support/hooks.rb:38:in `block (2 levels) in <top (required)>'
/myProject/features/support/hooks.rb:35:in `each'
/myProject/features/support/hooks.rb:35:in `Around'
Everything seems to be working as expected until the execution deals with the visit_page method that belongs to PageObject gem.
There are possibly two problems I see here.
First of all you are getting the error because you did not register the PageObject::PageFactory module with World. Add this line after the requires:
World(PageObject::PageFactory)
Second, you are using an Around block and calling the same Scenario multiple times. From what I am seeing, you are doing this so that it will exercise the same behavior with different logged in users. If the behavior is exactly the same, what are you accomplishing by running it three times?
Related
I am trying to build my Rspec testing test to test out a ruby app I am building. I know I should build first but test later. The code does work 100% I am just having issues getting Rspec to even look at my code.
The full code on github:
https://github.com/EsdrasEtrenne/tictactoe
The only file that im running with rspec so far is ruby/spec/game_spec.rb
the game_spec.rb file looks like this:
require_relative "../tictactoe"
Rspec.describe Tasks do
before(:each)do
#game = Tictactoe::Game.new
end
it "has a working method called play" do
expect{#game.play}.to output("WELCOME! To the unbeatable Tic-tac-toe").to_stdout
end
end
It requires tictactoe as a relative:
require "./components/tasks.rb"
require "./components/board.rb"
require "./components/player.rb"
require "./components/player_types/computer.rb"
require "./components/player_types/human.rb"
module Tictactoe
class Game
attr_reader :board, :player, :opponent, :tasks
def initialize
#board = Board.new
#tasks = Tasks.new
end
def play
#tasks.greet_players
#player, #opponent = #tasks.get_order
current_player, current_opponent = #player, #opponent
#tasks.print_board(#board)
until #board.game_is_over || #board.tie
#tasks.tell_turn(current_player)
current_player.move(#board)
#tasks.print_board(#board)
if #board.game_is_over || #board.tie
#tasks.game_over(#board)
if #tasks.end_options
game = Tictactoe::Game.new
game.play
end
else
current_player, current_opponent = current_opponent, current_player
end
end
end
end
end
game = Tictactoe::Game.new
game.play
Then I get this error when I run rspec game_spec.rb:
An error occurred while loading ./game_spec.rb.
Failure/Error: return gem_original_require(path)
LoadError:
cannot load such file -- ./components/tasks.rb
# /Users/Esdras/Desktop/first_vagrant_box/coding_challenges/ruby/tictactoe.rb:1:in `<top (required)>'
# ./game_spec.rb:1:in `require_relative'
# ./game_spec.rb:1:in `<top (required)>'
No examples found.
Finished in 0.00028 seconds (files took 0.08732 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
The game works 100% regularly. I just am looking to make the first test pass and from there the rest should be really straight forward.
the require paths are resolved according to the dir you're in when you're executing the code. It's actually a bit more complicated than that, and there is this whole concept of "load path" which is configurable. See What are the paths that "require" looks up by default?
From this line An error occurred while loading ./game_spec.rb. I'm figuring you've run cd spec then rspec ./game_spec.rb or something like that. I think your code would work if you were in the root of the project directory and ran rspec spec/game_spec.rb.
The benefit of require_relative over require is that the paths can be resolved no matter where you call the script from. I think if you used require_relative in tictactoe.rb it would work.
I am writing tests with minitest for a resque worker.
How do I write a test for the rescue block that gets called in case Resque gets terminated and sends a Resque::TermException to the worker?
My approach is to stub one of the methods called by the worker and have it raise a Resque::TermException instead.
Sometimes I can raise the exception, but it happens at the wrong time and kills the testrun. And now I tried something like this:
(I'm using fixtures and mocha)
test "if interrupted it removes the file" do
CSV.stub(:open).and_raise(Resque::TermException.new(15))
report = Report.find_by_id(1)
Report.expects(:find_by_id).returns(report)
ReportJob.perform({ 'test' => 'test' })
refute File.exist? ReportJob.report_file(report)
end
but no luck running it:
1) Error:
ReportTest#test_if_interrupted_it_removes_file:
ArgumentError: wrong number of arguments (1 for 2)
test/resque-tasks/spreadsheet_report_test.rb:2:in `block in <class:ReportTest>'
/Users/me/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/test/unit/testcase.rb:17:in `run'
/Users/me/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-3.1.10/lib/active_support/testing/setup_and_teardown.rb:36:in `block in run'
...
Mocha currently uses this syntax for stubs and raises:
object.stubs(:expected_method).raises(Exception, 'message')
See http://www.rubydoc.info/github/floehopper/mocha/Mocha/Expectation:raises
Are you perhaps using an older version of Mocha, with a different syntax?
Or is it possible you're accidentially using RSpec syntax?
I am following a tutorial and have been getting some errors, I believe some of the language is outdated with Rspec and have messed around alot to try and fix this but have not been able to.
The error i get is
NoMethodError:
undefined method `should_recieve' for #<Panda:0x007f9cd45c6458>
# ./spec/zookeeper_spec.rb:9:in `block (2 levels) in <top (required)>'
The code in question is
describe Zookeeper do
it "should be able to feed the bamboo to the pandas" do
panda = Panda.new
panda.should_recieve(:eat).with(:bamboo)
Zookeeper.new.feed(food: :bamboo, to: panda)
end
I have realised that should_recieve should actually be should_receive and this has resolved the problem. I then replaced should_receive with and expectation and to receive
describe Zookeeper do
it "should be able to feed the bamboo to the pandas" do
panda = Panda.new
expect(panda).to receive(:eat).with(:bamboo)
Zookeeper.new.feed(food: :bamboo, to: panda)
end
When running through the rspec course in codeschool I keep running into the same problem. I will set up as requested and after creating zombie_spec.rb and running rspec I get the proper output listed below:
Justins-MacBook-Pro:rubyproject Justin$ rspec spec/lib/zombie_spec.rb
Run options: include {:focus=>true}
All examples were filtered out; ignoring {:focus=>true}
*
Pending:
A Zombie is named Ash
# Not yet implemented
# ./spec/lib/zombie_spec.rb:3
Finished in 0.00929 seconds
1 example, 0 failures, 1 pending
Randomized with seed 7259
As I continue on with the first video and create the class Zombie as mentioned I receive this error when running rspec again:
Justins-MacBook-Pro:rubyproject Justin$ rspec spec/lib/zombie_spec.rb
/usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- zombie (LoadError)
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/Justin/rubyproject/spec/lib/zombie_spec.rb:2:in `<top (required)>'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
After this I removed everything and uninstalled and reinstalled rspec. Retried it again and returned the same results.
Any clue what is going on?
Thank you in advance for your help!
require_relative will load the file from the same directory as the rspec file
I had pretty much the same issue, though mine never worked even once at first. Updating the zombie_spec.rb file to show the full path of my zombie.rb file seemed to get it working properly.
Eg:require "/home/me/ruby/spec/lib/zombie"
I use this and it worked
require_relative "zombie.rb"
Well if this still a problem I got into the same issue so I created a folder under spec/lib where I put all the codes and another spec/test where goes all the tests then it worked.
and I also added require_relative here is the code snippet
zombie_spec.rb
require 'spec_helper'
require_relative '../lib/zombie'
describe Zombie do
it "has a name called'Jack'" do
zb = Zombie.new
zb.name.should == "Jack"
end
it "has no brains" do
zb = Zombie.new
zb.should be_intelligent == false
end
end
zombie.rb
class Zombie
attr_accessor :name
def initialize
#name = "Jack"
end
def intelligent?
false
end
end
I am using Rails 3.1, RSpec 2.6, and rr 1.0.4 and I get a NoMethodError:
undefined method `stub' for #<Activity:0x007faa90996190>
I am trying to utilize the RSpec test below to test my "Activities" show.haml view. If I change my spec_helper.rb file to use RSpec for mocks instead of rr then the test passes. I have tried changing the syntax of the code but to no success.
I found several websites stating that RSpec and rr do not "play well together and one person provided this rpsec-rr solution which did not work for me.
This is my show.haml_spec.rb
require 'spec_helper'
describe "activities/show.haml" do
before(:each) do
#activity = assign(:activity, stub_model(Activity))
end
it "renders attributes in .haml" do
render
end
end
This is the output from my Eclipse compiler using Aptana Studio
Failures:
1) activities/show.haml renders attributes in .haml
Failure/Error: #activity = assign(:activity, stub_model(Activity))
NoMethodError:
undefined method `stub' for #<Activity:0x007faa90996190>
# ./spec/views/activities/show.haml_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 0.15479 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/views/activities/show.haml_spec.rb:8 # activities/show.haml renders attributes in .haml
Any recommendation to an alternate syntax would be greatly appreciated!!
Note that as of RR 1.0.5 (released 3/28/2013) this problem should no longer be present -- RR is fully compatible with RSpec 2.