Actually it is not hard to use mock_model without rails. You need to install rspec-rails, and active_record gem first. Here is an example,
require 'active_support/core_ext/hash'
require 'spec/rails/mocks'
include Spec::Rails::Mocks
class TestTable
end
class View
def initialize(table)
@table = table
end
def print_keys
"name is #{@table.name} #{@table.class.primary_keys.join(",")}"
end
def print_name
"name is #{@table.name}"
end
end
describe "TestTable" do
before do
table = mock_model(TestTable, :name=>"table_abc")
@view = View.new(table)
end
it "should fail the expectation check" do
TestTable.should_receive(:primary_keys).and_return(["pk1", "pk2"])
@view.print_keys.should == "name is table_abc"
end
it "should complain that no primary_keys is called" do
TestTable.should_receive(:primary_keys).and_return(["pk1", "pk2"])
@view.print_name.should == "name is table_abc"
end
it "should print the table name" do
@view.print_name.should == "name is table_abc"
end
it "should print the table name and keys" do
TestTable.should_receive(:primary_keys).and_return(["pk1", "pk2"])
@view.print_keys.should == "name is table_abc pk1,pk2"
end
end
No comments:
Post a Comment