Created
July 11, 2013 11:46
-
-
Save snoblenet/5974761 to your computer and use it in GitHub Desktop.
competitor_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "spec_helper" | |
describe "competitor" do | |
before :each do | |
@element = Element.create! :name => "meta", :meta => true | |
@standard = Standard.create! :name => "meta", :meta => true, :description => "meta" | |
@search = Product.create! :name => "Retail Site Search" | |
@mispellings = @search.questions.create! :description => "Cope with mispellings?", :element_id => @element.id, :standard_id => @standard.id, :active => true | |
@review = @search.reviews.create! :client_name => "BHP", :url => "http://bhp.com.au" | |
@competitor = @review.competitors.create! :name => "Colonial FirstState", :url => "http://firststate.com.au" | |
end | |
describe "as guest" do | |
it "cannot list" do | |
visit review_competitors_path(@review) | |
page.current_path.should == root_path | |
end | |
end | |
describe "as customer" do | |
it "cannot list" do | |
login_as "customer" | |
visit review_competitors_path(@review) | |
page.current_path.should == root_path | |
end | |
end | |
describe "as tester" do | |
before :each do | |
login_as "tester" | |
visit review_competitors_path(@review) | |
end | |
it "can list" do | |
page.current_path.should == review_competitors_path(@review) | |
page.should have_content "List Competitors" | |
page.should have_content "BHP" | |
end | |
it "can show" do | |
click_link "Colonial FirstState" | |
page.should have_content "Colonial FirstState" | |
end | |
it "cannot edit" do | |
visit edit_review_competitor_path(@review, @competitor) | |
page.current_path.should == root_path | |
end | |
it "cannot delete" do | |
page.driver.submit :delete, review_competitor_path(@review, @competitor), {} | |
page.current_path.should == root_path | |
@competitor.destroyed?.should be_false | |
end | |
end | |
describe "as admin" do | |
before :each do | |
login_as "admin" | |
visit review_competitors_path(@review) | |
end | |
it "can create" do | |
click_on "new_competitor" | |
fill_in "competitor_name", :with => "ING" | |
fill_in "competitor_url", :with => "http://ing.com.au" | |
click_on "submit_competitor" | |
page.current_path.should == review_competitor_path(@review, Competitor.find_by_name("ING")) | |
page.should have_content "ING" | |
page.should have_content "http://ing.com.au" | |
page.should have_content "BHP" | |
end | |
it "can list" do | |
page.current_path.should == review_competitors_path(@review) | |
page.should have_content "List Competitors" | |
page.should have_content "BHP" | |
end | |
it "can edit and undo" do | |
click_link "Colonial FirstState" | |
click_link "edit_competitor" | |
fill_in "competitor_name", :with => "Sunsuper" | |
fill_in "competitor_url", :with => "http://sunsuper.com.au" | |
click_on "submit_competitor" | |
page.current_path.should == review_competitor_path(@review, Competitor.find_by_name("Sunsuper")) | |
page.should_not have_content "Colonial FirstState" | |
page.should have_content "Sunsuper" | |
page.should have_content "http://sunsuper.com.au" | |
click_link "Undo" | |
page.should have_content "Colonial FirstState" | |
page.should_not have_content "Sunsuper" | |
page.should_not have_content "http://sunsuper.com.au" | |
end | |
it "can delete and restore" do | |
click_link "Colonial FirstState" | |
click_link "delete_competitor" | |
page.current_path.should == review_competitors_path(@review) | |
page.should_not have_content "Colonial FirstState" | |
click_link "Undo" | |
page.should have_content "Colonial FirstState" | |
end | |
end | |
describe "methods" do | |
describe "create_matching_surveys_and_results" do | |
it "creates one survey for each non-iterated question" do | |
@competitor = @review.competitors.first | |
@competitor.questions.select{|question| question.description == "Cope with mispellings?"}.count.should == 1 | |
end | |
it "creates several surveys for each iterated question" do | |
@question = @search.questions.create! :description => "What is {{input-name}}?", :iterations => 2, :element_id => @element.id, :standard_id => @standard.id | |
@review = @search.reviews.create! :client_name => "Amazon", :url => "http://amazon.com" | |
@competitor = @review.competitors.first | |
@question.surveys.select{|survey| survey.competitor == @competitor}.count.should == 2 | |
end | |
end | |
describe "standards methods" do | |
before :each do | |
@usable = Standard.create! :name => "usable", :description => "usable" | |
@findable = Standard.create! :name => "findable", :description => "findable" | |
@persuasive = Standard.create! :name => "persuasive", :description => "persuasive" | |
@product = Product.create! :name => "product" | |
@otherreview = @product.reviews.create! :client_name => "other", :url => "http://other.com" | |
@rival = @otherreview.competitors.first | |
@question = @product.questions.create! :description => "question", :element_id => @element.id, :standard_id => @usable.id, :active => true | |
@otherquestion = @product.questions.create! :description => "question", :element_id => @element.id, :standard_id => @standard.id, :active => true | |
end | |
describe "has_standard?(standard)" do | |
it "return true or false as required" do | |
@rival.has_standard?(@standard).should be_true | |
@rival.has_standard?(@usable).should be_true | |
@rival.has_standard?(@findable).should be_false | |
@rival.has_standard?(@persuasive).should be_false | |
@competitor.has_standard?(@standard).should be_true | |
@competitor.has_standard?(@usable).should be_false | |
@competitor.has_standard?(@findable).should be_false | |
@competitor.has_standard?(@persuasive).should be_false | |
end | |
end | |
describe "standards" do | |
it "should return a sorted array" do | |
@rival.standards.should == [@standard, @usable] | |
@competitor.standards.should == [@standard] | |
end | |
end | |
end | |
describe "points methods" do | |
before :each do | |
@usable = Standard.create! :name => "usable", :description => "usable" | |
@findable = Standard.create! :name => "findable", :description => "findable" | |
@active_usable_question = @search.questions.create! :description => "active usable question", :element_id => @element.id, :standard_id => @usable.id, :active => true | |
@inactive_usable_question = @search.questions.create! :description => "inactive usable question", :element_id => @element.id, :standard_id => @usable.id | |
@active_findable_question = @search.questions.create! :description => "active findable question", :element_id => @element.id, :standard_id => @findable.id, :active => true | |
@inactive_findable_question = @search.questions.create! :description => "inactive findable question", :element_id => @element.id, :standard_id => @findable.id | |
@active_usable_field = @active_usable_question.fields.create! :name => "active usable field", :points => 1000.0 | |
@inactive_usable_field = @inactive_usable_question.fields.create! :name => "inactive usable field", :points => 100.0 | |
@active_findable_field = @active_findable_question.fields.create! :name => "active findable field", :points => 10.0 | |
@inactive_findable_field = @inactive_findable_question.fields.create! :name => "inactive findable field", :points => 1.0 | |
Result.all.select{|result| result.field_id == @active_usable_field.id && result.competitor == @competitor}.first.update_attributes! :selected => true | |
Result.all.select{|result| result.field_id == @inactive_usable_field.id && result.competitor == @competitor}.first.update_attributes! :selected => true | |
Result.all.select{|result| result.field_id == @active_findable_field.id && result.competitor == @competitor}.first.update_attributes! :selected => true | |
Result.all.select{|result| result.field_id == @inactive_findable_field.id && result.competitor == @competitor}.first.update_attributes! :selected => true | |
end | |
describe "available_points" do | |
it "should total the points correctly" do | |
@competitor.available_points.should == 1010.0 | |
end | |
end | |
describe "available_points_for_standard(standard)" do | |
it "should total the points correctly" do | |
@competitor.available_points_for_standard(@usable).should == 1000.0 | |
@competitor.available_points_for_standard(@findable).should == 10.0 | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment