Skip to content

Instantly share code, notes, and snippets.

@alvin2ye
Created August 6, 2009 06:26
Show Gist options
  • Save alvin2ye/163176 to your computer and use it in GitHub Desktop.
Save alvin2ye/163176 to your computer and use it in GitHub Desktop.
rspec model test
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
module FinanceRecordSpecHelper
def valid_attributes
{
:item_type => "YinsouRecord",
:currency => "CNY",
:price => 500.12,
:loading_bill => LoadingBill.first,
:remark => "remark",
:state => "可付"
}
end
end
describe FinanceRecord do
include FinanceRecordSpecHelper
before(:each) do
@record = FinanceRecord.new
end
context "should be invalid" do
it "empty" do
@record.should_not be_valid
end
%w{currency loading_bill state}.each do |field|
it "require #{field}" do
@record.should_not be_valid
@record.errors.on(field.to_sym).should == I18n.t("activerecord.errors.messages.blank")
end
end
it "item_type" do
%w{error nil}.each do |value|
@record.attributes = valid_attributes.merge(:item_type => value)
@record.should_not be_valid
@record.errors.on(:item_type).should == I18n.t("activerecord.errors.messages.inclusion")
end
end
end
it "valid" do
@record.attributes = valid_attributes
@record.should be_valid
end
it "belongs to loading_bill" do
FinanceRecord.first.loading_bill.should be_a_kind_of LoadingBill
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment