Skip to content

Instantly share code, notes, and snippets.

@chintanparikh
Created October 11, 2012 06:26
Show Gist options
  • Save chintanparikh/3870576 to your computer and use it in GitHub Desktop.
Save chintanparikh/3870576 to your computer and use it in GitHub Desktop.
Controller and Spec file
# --- SPEC FILE ----
describe "#create" do
before do
@create_hash = {
category_id: category.id,
section_id: section.id,
listing: FactoryGirl.attributes_for(:listing)
}
end
subject { post :create, @create_hash}
context "not logged in" do
it_should_behave_like "Unauthorized action"
end
context "Logged is as a user" do
before(:each) { sign_in user }
it "should create a new listing" do
expect { subject }.to change(Listing, :count).by(1)
end
specify { subject; assigns[:listing].section_id.should == section.id }
end
end
# ---- CONTROLLER ACTION ----
def create
@listing = Section.find(params[:section_id]).listings.create(params[:listing])
@listing.user = current_user
@listing.save
respond_with [@listing.section.category, @listing.section, @listing]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment