Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding separated test for rules and validator #1 #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion lib/rank_results/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ module RankResults
class Rule

def initialize(object)
raise 'Add a not nil object' if object.nil?
@object = object
end

def apply
raise "You must implement apply method"
raise 'You must implement apply method'
end

end
Expand Down
File renamed without changes.
12 changes: 10 additions & 2 deletions test/rank_results_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'helper'
Dir["test/rules/*.rb"].each {|file| require './' + file }
Dir["test/test_classes/*.rb"].each {|file| require './' + file }
Dir["test/classes/*.rb"].each {|file| require './' + file }

describe RankResults::Valuator do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should change this whole specs

I would expect something like this

  • Create rule mocks and stub apply method
  • Validate that rank returns the sum of applying rules...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide and example not sure I'm following you @solojavier

before do
Expand All @@ -13,7 +13,15 @@
@valuator.add_rule instructor_followed_rule
end

it "testing first rule" do
it "should return a LikeRule instance" do
@valuator.rules[0].must_be_instance_of LikesRule
end

it "should return a InstructorsFollowedRule instance" do
@valuator.rules[1].must_be_instance_of InstructorsFollowedRule
end

it "testing rank results" do
@valuator.rank.must_equal 1020
end

Expand Down
28 changes: 28 additions & 0 deletions test/rules_test/rule_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'helper'

class ExampleBrokenRule < RankResults::Rule
end

class ExampleRule < RankResults::Rule
def apply
@object.tickets_sold * 10
end
end

describe RankResults::Rule do
before do
@videos = [Video.new(tickets_sold: 2, likes: 3, user_id: 1), Video.new(tickets_sold: 4, likes: 1, user_id: 5)]
end

it "should raise an exception if apply is not defined" do
proc {ExampleBrokenRule.new(@videos[0]).apply}.must_raise RuntimeError
end

it "should return the value for the given rule" do
ExampleRule.new(@videos[0]).apply.must_equal 20
end

it "should do something with nil object" do
proc {ExampleRule.new(nil)}.must_raise RuntimeError
end
end