Created
November 1, 2012 16:56
-
-
Save shakthimaan/3995015 to your computer and use it in GitHub Desktop.
rspec mock association
This file contains 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
def order | |
@user = User.find(:first, :conditions => [ "name = ?", params[:id] ]) | |
if not @user | |
render :json => { :errors => "User #{@choice} doesn't exist" }, :status => 404 | |
else | |
@orders = @user.orders | |
if @orders.empty? | |
render :json => { :errors => "No orders" }, :status => 404 | |
else | |
respond_to do |format| | |
format.json { render :json => { @orders } | |
end | |
end | |
end | |
end |
This file contains 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
describe UsersController, "GET order list" do | |
context "if user doesn't exist" do | |
it "must return user doesn't exist" do | |
get :order, {:format => "json", :id => "foo"} | |
response.body.should =~ /doesn't exist/ | |
response.should_not be_ok | |
end | |
end | |
context "if user has no orders" do | |
it "must return no orders" do | |
@user = User.create!(:id => 1, :name => "foo") | |
get :order, {:format => "json", :id => "foo"} | |
response.body.should =~ /No orders/ | |
response.should_not be_ok | |
end | |
end | |
context "if user has orders" do | |
it "must return a list of orders" do | |
@user = User.create!(:id => 1, :name => "foo") | |
@order = Order.create!(:id => 1, :name => "2345678") | |
get :order, {:format => "json", :id => "foo"} | |
response.body.should =~ /2345678/ | |
response.should be_ok | |
end | |
end | |
after(:each) do | |
User.destroy_all | |
Order.destroy_all | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment