From a503ac305985ae89e57de588025d29c39b7218b6 Mon Sep 17 00:00:00 2001 From: Ernesto Tagwerker Date: Tue, 10 Sep 2024 19:30:03 -0400 Subject: [PATCH] Add controller spec to check post :create --- spec/controllers/gemmies_controllers_spec.rb | 33 ++++++++++++++++++++ spec/spec_helper.rb | 5 +++ 2 files changed, 38 insertions(+) create mode 100644 spec/controllers/gemmies_controllers_spec.rb diff --git a/spec/controllers/gemmies_controllers_spec.rb b/spec/controllers/gemmies_controllers_spec.rb new file mode 100644 index 0000000..7bbd00f --- /dev/null +++ b/spec/controllers/gemmies_controllers_spec.rb @@ -0,0 +1,33 @@ +require "rails_helper" + +RSpec.describe GemmiesController, type: :controller do + describe "create" do + context "when the gemmy params are valid" do + it "redirects to the new gemmy page" do + post :create, params: { gemmy: { name: "next_rails" } } + + expect(response).to redirect_to(gemmy_path(Gemmy.find_by(name: "next_rails"))) + end + + it "creates a record in the database" do + expect do + post :create, params: { gemmy: { name: "next_rails" } } + end.to change(Gemmy, :count).by(1) + end + + context "when the gemmy params are invalid" do + it "renders the new gemmy page" do + post :create, params: { gemmy: { name: "" } } + + expect(response).to render_template(:new) + end + + it "does not create a record in the database" do + expect do + post :create, params: { gemmy: { name: "" } } + end.not_to change(Gemmy, :count) + end + end + end + end +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 327b58e..b9c86d8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,8 @@ +if ENV["COVERAGE"] + require "simplecov" + SimpleCov.start "rails" +end + # This file was generated by the `rails generate rspec:install` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # The generated `.rspec` file contains `--require spec_helper` which will cause