Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
assignment_rspec_ruby_sprint
============================
### by Tim Scott


Testing, testing, 1, 2, 3

[A Ruby and RSpec testing assignment from the Viking Code School](http://www.vikingcodeschool.com)
[A Ruby and RSpec testing assignment from the Viking Code School](http://www.vikingcodeschool.com)
24 changes: 12 additions & 12 deletions lib/viking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ def check_death
end

# Run script
oleg = Viking.new("Oleg")
sven = Viking.new("Sven")
oleg.attack(sven)
oleg.pick_up_weapon(Bow.new(2))
3.times { oleg.attack(sven) }
sven.attack(oleg)
sven.pick_up_weapon(Axe.new)
3.times {sven.attack(oleg)}
puts "\nInspecting vikings:\n"
puts oleg.inspect
puts sven.inspect
puts "\n...thanks for playing.\n\n"
#oleg = Viking.new("Oleg")
#sven = Viking.new("Sven")
#oleg.attack(sven)
#oleg.pick_up_weapon(Bow.new(2))
#3.times { oleg.attack(sven) }
#sven.attack(oleg)
#sven.pick_up_weapon(Axe.new)
#3.times {sven.attack(oleg)}
#puts "\nInspecting vikings:\n"
#puts oleg.inspect
#puts sven.inspect
#puts "\n...thanks for playing.\n\n"
14 changes: 7 additions & 7 deletions lib/warmup.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
class Warmup

# This is a user input and command-line-output
# test. You need to stub out `gets` to make it work
# And can stub out `puts` just for fun
def gets_shout
shout = gets.chomp.upcase

# This is a side effect. Test it if you'd like!
# (optional).
# (optional).
puts shout

return shout
end


# For your spec for this one, pass in a plain old double
# that can accept a #size method and return something
# Otherwise, treat is as any other test (this should be
# Otherwise, treat it is as any other test (this should be
# an equality test, e.g. expect(...).to eq(...))
def triple_size(array)
return array.size * 3
end


# Tricky! This method has side effects. Pay attention to
# which object each method is called on. Remember that
# "bang!" methods are in-place (destructive).
#
# For this method, write separate tests to show:
# 1. The string you pass in receives the #upcase!
# method call,
# 2. The string you pass in receives the #reverse!
# method call,
# 2. The string you pass in receives the #reverse!
# method call,
# 3. Your method actually returns a completely different
# object than the one you passed in
Expand Down
34 changes: 33 additions & 1 deletion spec/bow_spec.rb
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
# Your code here
# Your code here
require 'weapons/bow'

describe Bow do
let(:bow) { Bow.new }

describe '#arrows' do
it 'is readable' do
expect{ bow.arrows }.to_not raise_error
end

it 'starts with default number of arrows' do
expect(bow.arrows).to eq(10)
end

it 'starts with the number of arrows passed in' do
big_bow = Bow.new(20)
expect(big_bow.arrows).to eq(20)
end
end

describe '#use' do
it 'reduces arrows by 1' do
bow.use
expect(bow.arrows).to eq(9)
end

it 'throws an error if no arrows' do
10.times { bow.use }
expect { bow.use }.to raise_error('Out of arrows')
end
end
end
103 changes: 103 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end

# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
# have no way to turn it off -- the option exists only for backwards
# compatibility in RSpec 3). It causes shared context metadata to be
# inherited by the metadata hash of host groups and examples, rather than
# triggering implicit auto-inclusion in groups with matching metadata.
config.shared_context_metadata_behavior = :apply_to_host_groups

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# This allows you to limit a spec run to individual examples or groups
# you care about by tagging them with `:focus` metadata. When nothing
# is tagged with `:focus`, all examples get run. RSpec also provides
# aliases for `it`, `describe`, and `context` that include `:focus`
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
config.filter_run_when_matching :focus

# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options. We recommend
# you configure your source control system to ignore this file.
config.example_status_persistence_file_path = "spec/examples.txt"

# Limits the available syntax to the non-monkey patched syntax that is
# recommended. For more details, see:
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
config.disable_monkey_patching!

# This setting enables warnings. It's recommended, but in some cases may
# be too noisy due to issues in dependencies.
config.warnings = true

# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
end

# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random

# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
=end
end
132 changes: 131 additions & 1 deletion spec/viking_spec.rb
Original file line number Diff line number Diff line change
@@ -1 +1,131 @@
# Your code here
# Your code here
require 'viking'

describe Viking do
let(:viking) { Viking.new }
let(:bow) { Bow.new }
let(:axe) { Axe.new }
let(:fists) { Fists.new }
let(:little_viking) { Viking.new }

describe '#name' do
it 'correctly sets name attribute' do
new_viking = Viking.new('Tim')
expect(new_viking.name).to eq('Tim')
end
end

describe '#health' do
it 'correctly sets health attribute' do
sick_viking = Viking.new('Tim', 50)
expect(sick_viking.health).to eq(50)
end
it 'cannot be overwritten after it has been set' do
expect { viking.health = 200 }.to raise_error(NoMethodError)
end
end

describe '#weapon' do
it 'starts nil by default' do
expect(viking.weapon).to eq(nil)
end
end

describe '#pick_up_weapon' do
it 'sets weapon to picked up weapon' do
viking.pick_up_weapon(bow)
expect(viking.weapon).to eq(bow)
end
it 'raises error when picking up non-weapon' do
expect{ viking.pick_up_weapon('hamburger') }.to raise_error("Can't pick up that thing")
end
it 'replaces old weapon with new weapon' do
viking.pick_up_weapon(bow)
viking.pick_up_weapon(axe)
expect(viking.weapon).to eq(axe)
end
end

describe '#drop_weapon' do
it 'leaves the viking weaponless' do
viking.pick_up_weapon(bow)
viking.drop_weapon
expect(viking.weapon).to eq(nil)
end
end

describe '#receive_attack' do
it "reduces Viking's health correct amount" do
viking.receive_attack(10)
expect(viking.health).to eq(90)
end
it 'calls the #take_damage method' do
expect(viking).to receive(:take_damage)
viking.receive_attack(10)
end
end

describe '#attack' do
context 'when attacking another viking' do
it "decreases the attacked viking's health" do
viking.attack(little_viking)
expect(little_viking.health).to be <(100)
end
it "calls that viking's #take_damage method" do
expect(little_viking).to receive(:take_damage)
viking.attack(little_viking)
end
end
context "when attacking with no weapon" do
it "runs #damage_with_fists" do
expect(viking).to receive(:damage_with_fists).and_return(10)
viking.attack(little_viking)
end
it "deals Fists multiplier times strength damage" do
multiplier = 0.25
strength = viking.strength
health = viking.health
viking.attack(little_viking)
difference = health - little_viking.health
product = multiplier * strength
expect(difference).to eq(product)
end
end
context "when attacking with a weapon" do
it "runs #damage_with_weapon" do
allow(viking).to receive(:damage_with_weapon).and_return(55)
expect(viking).to receive(:damage_with_weapon)
viking.pick_up_weapon(axe)
viking.attack(little_viking)
end
it "deals damage equal to strength * multiplier" do
viking.pick_up_weapon(axe)
multiplier = 1
strength = viking.strength
health = little_viking.health
viking.attack(little_viking)
difference = health - little_viking.health
product = multiplier * strength
expect(difference).to eq(product)
end
end
context "when attacking with a bow" do
it "uses fists when enough arrows aren't available" do
10.times { bow.use }
viking.pick_up_weapon(bow)
expect(little_viking).to_not receive(:damage_with_fists)
viking.attack(little_viking)
end
end

describe '#check_death' do
context "when killing a viking" do
it 'raises an error' do
expect do
100.times { viking.attack(little_viking) }.to raise_error("#{little_viking.name} has Died...")
end
end
end
end
end
end
37 changes: 37 additions & 0 deletions spec/warmup_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'warmup'

describe Warmup do
let(:warmup) { Warmup.new }

describe "#gets_shout" do
it "returns a shouted message" do
allow(warmup).to receive(:gets).and_return('hey, world')
expect(warmup.gets_shout).to eq("HEY, WORLD")
end
end

describe '#triple_size' do
it 'returns three times the size of the array' do
array = double(size: 5)

expect(warmup.triple_size(array)).to eq(15)
end
end

describe '#calls_some_methods' do
let(:loud_string) { double(reverse!: "OLLEH") }
let(:string) { double(empty?: false, upcase!: loud_string, reverse!: "OLLEH") }

it 'receives the #upcase! method call' do
expect(string).to receive(:upcase!)
warmup.calls_some_methods(string)
end
it 'receives the #reverse! method call' do
expect(loud_string).to receive(:reverse!)
warmup.calls_some_methods(string)
end
it 'returns a completely different object' do
expect(warmup.calls_some_methods(string)).to_not eq(string)
end
end
end