From 6b1a1834ad1715145f047790c8391b8a5558bece Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Wed, 20 Dec 2023 20:34:25 +0000 Subject: Created new Operation model and fixed Event associations with it --- spec/factories/operations.rb | 10 ++ spec/helpers/operations_helper_spec.rb | 15 +++ spec/models/operation_spec.rb | 33 +++++++ spec/requests/operations_spec.rb | 135 +++++++++++++++++++++++++++ spec/routing/operations_routing_spec.rb | 38 ++++++++ spec/views/operations/edit.html.erb_spec.rb | 22 +++++ spec/views/operations/index.html.erb_spec.rb | 20 ++++ spec/views/operations/new.html.erb_spec.rb | 18 ++++ spec/views/operations/show.html.erb_spec.rb | 14 +++ 9 files changed, 305 insertions(+) create mode 100644 spec/factories/operations.rb create mode 100644 spec/helpers/operations_helper_spec.rb create mode 100644 spec/models/operation_spec.rb create mode 100644 spec/requests/operations_spec.rb create mode 100644 spec/routing/operations_routing_spec.rb create mode 100644 spec/views/operations/edit.html.erb_spec.rb create mode 100644 spec/views/operations/index.html.erb_spec.rb create mode 100644 spec/views/operations/new.html.erb_spec.rb create mode 100644 spec/views/operations/show.html.erb_spec.rb (limited to 'spec') diff --git a/spec/factories/operations.rb b/spec/factories/operations.rb new file mode 100644 index 0000000..87ee08f --- /dev/null +++ b/spec/factories/operations.rb @@ -0,0 +1,10 @@ +FactoryBot.define do + factory :operation do + name { "MyString" } + organisation + end + + factory :organisation do + name { "Singo Ltd" } + end +end diff --git a/spec/helpers/operations_helper_spec.rb b/spec/helpers/operations_helper_spec.rb new file mode 100644 index 0000000..b0d4550 --- /dev/null +++ b/spec/helpers/operations_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the OperationsHelper. For example: +# +# describe OperationsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe OperationsHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/operation_spec.rb b/spec/models/operation_spec.rb new file mode 100644 index 0000000..a46a528 --- /dev/null +++ b/spec/models/operation_spec.rb @@ -0,0 +1,33 @@ +require 'rails_helper' + +RSpec.describe Operation, type: :model do + let(:organisation) { + org = build(:organisation) + # Operation.create!( + # name: "MyString" + # ) + } + + # before(:each) do + # assign(:operation, operation) + # end + subject { described_class.new(name: "Spuds", organisation: organisation) } + + describe "existence" do + it "exists!" do + expect(subject).to be_valid + end + end + + describe "validations" do + it "is invalid without a name" do + subject.name = nil + expect(subject).to be_invalid + end + + it "is invalid with a name longer than 35 chars" do + subject.name = "a" * 36 + expect(subject).to be_invalid + end + end +end diff --git a/spec/requests/operations_spec.rb b/spec/requests/operations_spec.rb new file mode 100644 index 0000000..1bb4111 --- /dev/null +++ b/spec/requests/operations_spec.rb @@ -0,0 +1,135 @@ +require 'rails_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to test the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. + +RSpec.describe "/operations", type: :request do + + # This should return the minimal set of attributes required to create a valid + # Operation. As you add validations to Operation, be sure to + # adjust the attributes here as well. + let(:valid_attributes) { + skip("Add a hash of attributes valid for your model") + } + + let(:invalid_attributes) { + skip("Add a hash of attributes invalid for your model") + } + + describe "GET /index" do + it "renders a successful response" do + Operation.create! valid_attributes + get operations_url + expect(response).to be_successful + end + end + + describe "GET /show" do + it "renders a successful response" do + operation = Operation.create! valid_attributes + get operation_url(operation) + expect(response).to be_successful + end + end + + describe "GET /new" do + it "renders a successful response" do + get new_operation_url + expect(response).to be_successful + end + end + + describe "GET /edit" do + it "renders a successful response" do + operation = Operation.create! valid_attributes + get edit_operation_url(operation) + expect(response).to be_successful + end + end + + describe "POST /create" do + context "with valid parameters" do + it "creates a new Operation" do + expect { + post operations_url, params: { operation: valid_attributes } + }.to change(Operation, :count).by(1) + end + + it "redirects to the created operation" do + post operations_url, params: { operation: valid_attributes } + expect(response).to redirect_to(operation_url(Operation.last)) + end + end + + context "with invalid parameters" do + it "does not create a new Operation" do + expect { + post operations_url, params: { operation: invalid_attributes } + }.to change(Operation, :count).by(0) + end + + + it "renders a response with 422 status (i.e. to display the 'new' template)" do + post operations_url, params: { operation: invalid_attributes } + expect(response).to have_http_status(:unprocessable_entity) + end + + end + end + + describe "PATCH /update" do + context "with valid parameters" do + let(:new_attributes) { + skip("Add a hash of attributes valid for your model") + } + + it "updates the requested operation" do + operation = Operation.create! valid_attributes + patch operation_url(operation), params: { operation: new_attributes } + operation.reload + skip("Add assertions for updated state") + end + + it "redirects to the operation" do + operation = Operation.create! valid_attributes + patch operation_url(operation), params: { operation: new_attributes } + operation.reload + expect(response).to redirect_to(operation_url(operation)) + end + end + + context "with invalid parameters" do + + it "renders a response with 422 status (i.e. to display the 'edit' template)" do + operation = Operation.create! valid_attributes + patch operation_url(operation), params: { operation: invalid_attributes } + expect(response).to have_http_status(:unprocessable_entity) + end + + end + end + + describe "DELETE /destroy" do + it "destroys the requested operation" do + operation = Operation.create! valid_attributes + expect { + delete operation_url(operation) + }.to change(Operation, :count).by(-1) + end + + it "redirects to the operations list" do + operation = Operation.create! valid_attributes + delete operation_url(operation) + expect(response).to redirect_to(operations_url) + end + end +end diff --git a/spec/routing/operations_routing_spec.rb b/spec/routing/operations_routing_spec.rb new file mode 100644 index 0000000..e323736 --- /dev/null +++ b/spec/routing/operations_routing_spec.rb @@ -0,0 +1,38 @@ +require "rails_helper" + +RSpec.describe OperationsController, type: :routing do + describe "routing" do + it "routes to #index" do + expect(get: "/operations").to route_to("operations#index") + end + + it "routes to #new" do + expect(get: "/operations/new").to route_to("operations#new") + end + + it "routes to #show" do + expect(get: "/operations/1").to route_to("operations#show", id: "1") + end + + it "routes to #edit" do + expect(get: "/operations/1/edit").to route_to("operations#edit", id: "1") + end + + + it "routes to #create" do + expect(post: "/operations").to route_to("operations#create") + end + + it "routes to #update via PUT" do + expect(put: "/operations/1").to route_to("operations#update", id: "1") + end + + it "routes to #update via PATCH" do + expect(patch: "/operations/1").to route_to("operations#update", id: "1") + end + + it "routes to #destroy" do + expect(delete: "/operations/1").to route_to("operations#destroy", id: "1") + end + end +end diff --git a/spec/views/operations/edit.html.erb_spec.rb b/spec/views/operations/edit.html.erb_spec.rb new file mode 100644 index 0000000..3a2b5f0 --- /dev/null +++ b/spec/views/operations/edit.html.erb_spec.rb @@ -0,0 +1,22 @@ +require 'rails_helper' + +RSpec.describe "operations/edit", type: :view do + let(:operation) { + Operation.create!( + name: "MyString" + ) + } + + before(:each) do + assign(:operation, operation) + end + + it "renders the edit operation form" do + render + + assert_select "form[action=?][method=?]", operation_path(operation), "post" do + + assert_select "input[name=?]", "operation[name]" + end + end +end diff --git a/spec/views/operations/index.html.erb_spec.rb b/spec/views/operations/index.html.erb_spec.rb new file mode 100644 index 0000000..5fa64fe --- /dev/null +++ b/spec/views/operations/index.html.erb_spec.rb @@ -0,0 +1,20 @@ +require 'rails_helper' + +RSpec.describe "operations/index", type: :view do + before(:each) do + assign(:operations, [ + Operation.create!( + name: "Name" + ), + Operation.create!( + name: "Name" + ) + ]) + end + + it "renders a list of operations" do + render + cell_selector = Rails::VERSION::STRING >= '7' ? 'div>p' : 'tr>td' + assert_select cell_selector, text: Regexp.new("Name".to_s), count: 2 + end +end diff --git a/spec/views/operations/new.html.erb_spec.rb b/spec/views/operations/new.html.erb_spec.rb new file mode 100644 index 0000000..cea6080 --- /dev/null +++ b/spec/views/operations/new.html.erb_spec.rb @@ -0,0 +1,18 @@ +require 'rails_helper' + +RSpec.describe "operations/new", type: :view do + before(:each) do + assign(:operation, Operation.new( + name: "MyString" + )) + end + + it "renders new operation form" do + render + + assert_select "form[action=?][method=?]", operations_path, "post" do + + assert_select "input[name=?]", "operation[name]" + end + end +end diff --git a/spec/views/operations/show.html.erb_spec.rb b/spec/views/operations/show.html.erb_spec.rb new file mode 100644 index 0000000..7affaf4 --- /dev/null +++ b/spec/views/operations/show.html.erb_spec.rb @@ -0,0 +1,14 @@ +require 'rails_helper' + +RSpec.describe "operations/show", type: :view do + before(:each) do + assign(:operation, Operation.create!( + name: "Name" + )) + end + + it "renders attributes in

" do + render + expect(rendered).to match(/Name/) + end +end -- cgit v1.2.3