Created
April 24, 2012 14:50
-
-
Save wnstn/2480227 to your computer and use it in GitHub Desktop.
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 Answer do | |
| describe "in general" do | |
| before(:all) do | |
| site = Factory(:site) | |
| page = Factory(:page) | |
| @answer = Factory(:answer, :page_id => page.id, :site_id => site.id) | |
| subject {@answer} | |
| end | |
| it { should belong_to(:page) } | |
| it { should validate_presence_of(:site_id) } | |
| it { should validate_presence_of(:page_id) } | |
| it { should respond_to(:published_at) } | |
| it { should respond_to(:question) } | |
| it { should respond_to(:answer) } | |
| end | |
| describe "create answer" do | |
| before(:each) do | |
| @site = Factory(:site) | |
| @page = Factory(:page) | |
| end | |
| it "should create a question and answer for site" do | |
| @answer = Factory.build(:answer, :page_id => @page.id, :question => "Question 1", :site_id => @site.id) | |
| @answer.question.should include("Question 1") | |
| end | |
| it "with page_id should be valid" do | |
| Factory.build(:answer, :page_id => @page.id, :site_id => @site.id).should be_valid | |
| end | |
| it "without page_id should not be valid" do | |
| Factory.build(:answer, :site_id => @site.id).should_not be_valid | |
| end | |
| end | |
| describe "answer for specified slug" do | |
| before(:each) do | |
| site = Factory(:site) | |
| @page = Factory(:page) | |
| @page2 = Factory(:page) | |
| @answer = Factory(:answer, :page_id => @page.id, :question => "Question 1", :site_id => site.id) | |
| @answer2 = Factory(:answer, :page_id => @page2.id, :question => "Question 2", :site_id => site.id) | |
| end | |
| it "should not include any answer without given parent_slug" do | |
| Answer.for_parent_id(@page.id).should_not include(@answer2) | |
| end | |
| it "should include any answer that has the given parent_slug" do | |
| Answer.for_parent_id(@page.id).should include(@answer) | |
| end | |
| end | |
| end |
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
| class CreateAnswers < ActiveRecord::Migration | |
| def self.up | |
| create_table :answers do |t| | |
| t.integer :site_id, :null => false | |
| t.integer :page_id :null => false | |
| t.string :slug | |
| t.datetime :published_at | |
| t.string :question | |
| t.text :answer | |
| t.timestamps | |
| end | |
| add_index :answers, [:parent_slug, :slug], :unique => true | |
| add_foreign_key(:answers, :sites) | |
| end | |
| def self.down | |
| drop_table :answers | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment