TDD for Ruby On Rails API for group event items











up vote
1
down vote

favorite












I found myself working in this coding exercise for a work offer. I tried to apply TDD in the process but I didn't get any feedback from the company and I really would like to know what can be improved (pretty sure there are a lot of things). So any advice or refactor suggestion will be very much appreciated:




Background:



A group event will be created by an user. The group event should run for a whole number of days e.g.. 30 or 60. There should be attributes to set and update the start, end or duration of the event (and calculate the other value). The event also has a name, description (which supports formatting) and location. The event should be draft or published. To publish all of the fields are required, it can be saved with only a subset of fields before it’s published. When the event is deleted/remove it should be kept in the database and marked as such.



Deliverable:



Write an AR model, spec and migration for a GroupEvent that would meet the needs of the description above. Then write the api controller and spec to support JSON request/responses to manage these GroupEvents. For the purposes of this exercise, ignore auth.




Here is my app/models/group_event.rb:



class GroupEvent < ApplicationRecord
validates :name, :description, :starts_at, :ends_at, :location, presence: true, if: :published?
validate :duration_range_is_valid?, if: :published?
before_save :set_duration, if: :duration_range_is_valid?

def draft?
!published?
end

def check_duration_range
errors.add(:start_date, "Start date must be before end date") unless duration_range_is_valid?
end

def has_duration_range?
starts_at.present? && ends_at.present?
end

def duration_range_is_valid?
has_duration_range? and starts_at <= ends_at
end

def set_duration
self.duration = ((self.ends_at - self.starts_at) / 1.day).to_i if duration_range_is_valid?
end

def destroy
update_attribute(:deleted, true)
end
end


Here my app/controllers/api/v1/group_events_controller.rb:



    class Api::V1::GroupEventsController < ApplicationController
include Response
include ExceptionHandler

before_action :set_group_event, only: [:show, :update, :destroy]

# GET /api/v1/group_events
def index
@group_events = GroupEvent.all
json_response @group_events
end

# GET /api/v1/group_events/1
def show
json_response @group_event
end

# POST /api/v1/group_events
def create
@group_event = GroupEvent.create!(group_event_params)
json_response(@group_event, :created)
end

# PATCH/PUT /api/v1/group_events/1
def update
if @group_event.update(group_event_params)
json_response @group_event
else
json_response @group_event.errors, :unprocessable_entity
end
end

# DELETE /api/v1/group_events/1
def destroy
@group_event.destroy
end

private
def set_group_event
@group_event = GroupEvent.find(params[:id])
end

def group_event_params
params.permit(:name, :description, :deleted, :published, :location, :starts_at, :ends_at)
end
end


My config/routes.rb:



Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :group_events
end
end
end


And last but not least here is my spec/models/group_event_spec.rb:



require 'rails_helper'

RSpec.describe GroupEvent, type: :model do
it 'has a valid factory' do
expect(create(:group_event)).to be_valid
end

describe 'validations' do
context 'published' do
before { allow(subject).to receive(:published?).and_return(true) }
it { is_expected.to validate_presence_of :name }
it { is_expected.to validate_presence_of :description }
it { is_expected.to validate_presence_of :starts_at }
it { is_expected.to validate_presence_of :ends_at }
it { is_expected.to validate_presence_of :location }
end
context 'draft' do
before { allow(subject).to receive(:draft?).and_return(true) }
it { is_expected.not_to validate_presence_of :name }
it { is_expected.not_to validate_presence_of :description }
it { is_expected.not_to validate_presence_of :starts_at }
it { is_expected.not_to validate_presence_of :ends_at }
it { is_expected.not_to validate_presence_of :location }
end
end

describe '.draft?' do
context 'published' do
before { allow(subject).to receive(:published?).and_return(true) }
it 'is false' do
expect(subject.draft?).to eq(false)
end
end
context 'draft' do
before { allow(subject).to receive(:published?).and_return(false) }
it 'is true' do
expect(subject.draft?).to eq(true)
end
end
end

describe '.has_duration_range?' do
context 'with both values' do
before { allow(subject).to receive(:starts_at).and_return(DateTime.now) }
before { allow(subject).to receive(:ends_at).and_return(DateTime.tomorrow) }
it 'is true' do
expect(subject.has_duration_range?).to eq(true)
end
end
context 'without both values' do
before { allow(subject).to receive(:starts_at).and_return(nil) }
before { allow(subject).to receive(:ends_at).and_return(nil) }
it 'is false' do
expect(subject.has_duration_range?).to eq(false)
end
end
context 'without only one value' do
before { allow(subject).to receive(:starts_at).and_return(DateTime.now) }
before { allow(subject).to receive(:ends_at).and_return(nil) }
it 'is false' do
expect(subject.has_duration_range?).to eq(false)
end
end
end

describe '.duration_range_is_valid?' do
context 'with valid range' do
before { allow(subject).to receive(:has_duration_range?).and_return(true) }
before { allow(subject).to receive(:starts_at).and_return(DateTime.now) }
before { allow(subject).to receive(:ends_at).and_return(DateTime.tomorrow) }
it 'is true' do
expect(subject.duration_range_is_valid?).to eq(true)
end
end
context 'without invalid range' do
before { allow(subject).to receive(:has_duration_range?).and_return(true) }
before { allow(subject).to receive(:starts_at).and_return(DateTime.tomorrow) }
before { allow(subject).to receive(:ends_at).and_return(DateTime.now) }
it 'is false' do
expect(subject.duration_range_is_valid?).to eq(false)
end
end
end

describe '.set_duration' do
it 'with valid range' do
group_event = GroupEvent.create!(starts_at: DateTime.now, ends_at: DateTime.now + 10.days)
expect(group_event.duration).to eq(10)
end
it 'with invalid range' do
group_event = GroupEvent.create!(starts_at: DateTime.now, ends_at: DateTime.now - 10.days)
expect(group_event.duration).to eq(nil)
end
end

describe '.destroy' do
it 'does not destroy the event' do
group_event = GroupEvent.create!
count = GroupEvent.all.count
group_event.destroy
expect(GroupEvent.unscoped.count).to eq(count)
end
it 'sets deleted' do
group_event = GroupEvent.create!
group_event.destroy
expect(group_event.deleted).to eq(true)
end
end
end









share|improve this question









New contributor




Ibrah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    1
    down vote

    favorite












    I found myself working in this coding exercise for a work offer. I tried to apply TDD in the process but I didn't get any feedback from the company and I really would like to know what can be improved (pretty sure there are a lot of things). So any advice or refactor suggestion will be very much appreciated:




    Background:



    A group event will be created by an user. The group event should run for a whole number of days e.g.. 30 or 60. There should be attributes to set and update the start, end or duration of the event (and calculate the other value). The event also has a name, description (which supports formatting) and location. The event should be draft or published. To publish all of the fields are required, it can be saved with only a subset of fields before it’s published. When the event is deleted/remove it should be kept in the database and marked as such.



    Deliverable:



    Write an AR model, spec and migration for a GroupEvent that would meet the needs of the description above. Then write the api controller and spec to support JSON request/responses to manage these GroupEvents. For the purposes of this exercise, ignore auth.




    Here is my app/models/group_event.rb:



    class GroupEvent < ApplicationRecord
    validates :name, :description, :starts_at, :ends_at, :location, presence: true, if: :published?
    validate :duration_range_is_valid?, if: :published?
    before_save :set_duration, if: :duration_range_is_valid?

    def draft?
    !published?
    end

    def check_duration_range
    errors.add(:start_date, "Start date must be before end date") unless duration_range_is_valid?
    end

    def has_duration_range?
    starts_at.present? && ends_at.present?
    end

    def duration_range_is_valid?
    has_duration_range? and starts_at <= ends_at
    end

    def set_duration
    self.duration = ((self.ends_at - self.starts_at) / 1.day).to_i if duration_range_is_valid?
    end

    def destroy
    update_attribute(:deleted, true)
    end
    end


    Here my app/controllers/api/v1/group_events_controller.rb:



        class Api::V1::GroupEventsController < ApplicationController
    include Response
    include ExceptionHandler

    before_action :set_group_event, only: [:show, :update, :destroy]

    # GET /api/v1/group_events
    def index
    @group_events = GroupEvent.all
    json_response @group_events
    end

    # GET /api/v1/group_events/1
    def show
    json_response @group_event
    end

    # POST /api/v1/group_events
    def create
    @group_event = GroupEvent.create!(group_event_params)
    json_response(@group_event, :created)
    end

    # PATCH/PUT /api/v1/group_events/1
    def update
    if @group_event.update(group_event_params)
    json_response @group_event
    else
    json_response @group_event.errors, :unprocessable_entity
    end
    end

    # DELETE /api/v1/group_events/1
    def destroy
    @group_event.destroy
    end

    private
    def set_group_event
    @group_event = GroupEvent.find(params[:id])
    end

    def group_event_params
    params.permit(:name, :description, :deleted, :published, :location, :starts_at, :ends_at)
    end
    end


    My config/routes.rb:



    Rails.application.routes.draw do
    namespace :api do
    namespace :v1 do
    resources :group_events
    end
    end
    end


    And last but not least here is my spec/models/group_event_spec.rb:



    require 'rails_helper'

    RSpec.describe GroupEvent, type: :model do
    it 'has a valid factory' do
    expect(create(:group_event)).to be_valid
    end

    describe 'validations' do
    context 'published' do
    before { allow(subject).to receive(:published?).and_return(true) }
    it { is_expected.to validate_presence_of :name }
    it { is_expected.to validate_presence_of :description }
    it { is_expected.to validate_presence_of :starts_at }
    it { is_expected.to validate_presence_of :ends_at }
    it { is_expected.to validate_presence_of :location }
    end
    context 'draft' do
    before { allow(subject).to receive(:draft?).and_return(true) }
    it { is_expected.not_to validate_presence_of :name }
    it { is_expected.not_to validate_presence_of :description }
    it { is_expected.not_to validate_presence_of :starts_at }
    it { is_expected.not_to validate_presence_of :ends_at }
    it { is_expected.not_to validate_presence_of :location }
    end
    end

    describe '.draft?' do
    context 'published' do
    before { allow(subject).to receive(:published?).and_return(true) }
    it 'is false' do
    expect(subject.draft?).to eq(false)
    end
    end
    context 'draft' do
    before { allow(subject).to receive(:published?).and_return(false) }
    it 'is true' do
    expect(subject.draft?).to eq(true)
    end
    end
    end

    describe '.has_duration_range?' do
    context 'with both values' do
    before { allow(subject).to receive(:starts_at).and_return(DateTime.now) }
    before { allow(subject).to receive(:ends_at).and_return(DateTime.tomorrow) }
    it 'is true' do
    expect(subject.has_duration_range?).to eq(true)
    end
    end
    context 'without both values' do
    before { allow(subject).to receive(:starts_at).and_return(nil) }
    before { allow(subject).to receive(:ends_at).and_return(nil) }
    it 'is false' do
    expect(subject.has_duration_range?).to eq(false)
    end
    end
    context 'without only one value' do
    before { allow(subject).to receive(:starts_at).and_return(DateTime.now) }
    before { allow(subject).to receive(:ends_at).and_return(nil) }
    it 'is false' do
    expect(subject.has_duration_range?).to eq(false)
    end
    end
    end

    describe '.duration_range_is_valid?' do
    context 'with valid range' do
    before { allow(subject).to receive(:has_duration_range?).and_return(true) }
    before { allow(subject).to receive(:starts_at).and_return(DateTime.now) }
    before { allow(subject).to receive(:ends_at).and_return(DateTime.tomorrow) }
    it 'is true' do
    expect(subject.duration_range_is_valid?).to eq(true)
    end
    end
    context 'without invalid range' do
    before { allow(subject).to receive(:has_duration_range?).and_return(true) }
    before { allow(subject).to receive(:starts_at).and_return(DateTime.tomorrow) }
    before { allow(subject).to receive(:ends_at).and_return(DateTime.now) }
    it 'is false' do
    expect(subject.duration_range_is_valid?).to eq(false)
    end
    end
    end

    describe '.set_duration' do
    it 'with valid range' do
    group_event = GroupEvent.create!(starts_at: DateTime.now, ends_at: DateTime.now + 10.days)
    expect(group_event.duration).to eq(10)
    end
    it 'with invalid range' do
    group_event = GroupEvent.create!(starts_at: DateTime.now, ends_at: DateTime.now - 10.days)
    expect(group_event.duration).to eq(nil)
    end
    end

    describe '.destroy' do
    it 'does not destroy the event' do
    group_event = GroupEvent.create!
    count = GroupEvent.all.count
    group_event.destroy
    expect(GroupEvent.unscoped.count).to eq(count)
    end
    it 'sets deleted' do
    group_event = GroupEvent.create!
    group_event.destroy
    expect(group_event.deleted).to eq(true)
    end
    end
    end









    share|improve this question









    New contributor




    Ibrah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I found myself working in this coding exercise for a work offer. I tried to apply TDD in the process but I didn't get any feedback from the company and I really would like to know what can be improved (pretty sure there are a lot of things). So any advice or refactor suggestion will be very much appreciated:




      Background:



      A group event will be created by an user. The group event should run for a whole number of days e.g.. 30 or 60. There should be attributes to set and update the start, end or duration of the event (and calculate the other value). The event also has a name, description (which supports formatting) and location. The event should be draft or published. To publish all of the fields are required, it can be saved with only a subset of fields before it’s published. When the event is deleted/remove it should be kept in the database and marked as such.



      Deliverable:



      Write an AR model, spec and migration for a GroupEvent that would meet the needs of the description above. Then write the api controller and spec to support JSON request/responses to manage these GroupEvents. For the purposes of this exercise, ignore auth.




      Here is my app/models/group_event.rb:



      class GroupEvent < ApplicationRecord
      validates :name, :description, :starts_at, :ends_at, :location, presence: true, if: :published?
      validate :duration_range_is_valid?, if: :published?
      before_save :set_duration, if: :duration_range_is_valid?

      def draft?
      !published?
      end

      def check_duration_range
      errors.add(:start_date, "Start date must be before end date") unless duration_range_is_valid?
      end

      def has_duration_range?
      starts_at.present? && ends_at.present?
      end

      def duration_range_is_valid?
      has_duration_range? and starts_at <= ends_at
      end

      def set_duration
      self.duration = ((self.ends_at - self.starts_at) / 1.day).to_i if duration_range_is_valid?
      end

      def destroy
      update_attribute(:deleted, true)
      end
      end


      Here my app/controllers/api/v1/group_events_controller.rb:



          class Api::V1::GroupEventsController < ApplicationController
      include Response
      include ExceptionHandler

      before_action :set_group_event, only: [:show, :update, :destroy]

      # GET /api/v1/group_events
      def index
      @group_events = GroupEvent.all
      json_response @group_events
      end

      # GET /api/v1/group_events/1
      def show
      json_response @group_event
      end

      # POST /api/v1/group_events
      def create
      @group_event = GroupEvent.create!(group_event_params)
      json_response(@group_event, :created)
      end

      # PATCH/PUT /api/v1/group_events/1
      def update
      if @group_event.update(group_event_params)
      json_response @group_event
      else
      json_response @group_event.errors, :unprocessable_entity
      end
      end

      # DELETE /api/v1/group_events/1
      def destroy
      @group_event.destroy
      end

      private
      def set_group_event
      @group_event = GroupEvent.find(params[:id])
      end

      def group_event_params
      params.permit(:name, :description, :deleted, :published, :location, :starts_at, :ends_at)
      end
      end


      My config/routes.rb:



      Rails.application.routes.draw do
      namespace :api do
      namespace :v1 do
      resources :group_events
      end
      end
      end


      And last but not least here is my spec/models/group_event_spec.rb:



      require 'rails_helper'

      RSpec.describe GroupEvent, type: :model do
      it 'has a valid factory' do
      expect(create(:group_event)).to be_valid
      end

      describe 'validations' do
      context 'published' do
      before { allow(subject).to receive(:published?).and_return(true) }
      it { is_expected.to validate_presence_of :name }
      it { is_expected.to validate_presence_of :description }
      it { is_expected.to validate_presence_of :starts_at }
      it { is_expected.to validate_presence_of :ends_at }
      it { is_expected.to validate_presence_of :location }
      end
      context 'draft' do
      before { allow(subject).to receive(:draft?).and_return(true) }
      it { is_expected.not_to validate_presence_of :name }
      it { is_expected.not_to validate_presence_of :description }
      it { is_expected.not_to validate_presence_of :starts_at }
      it { is_expected.not_to validate_presence_of :ends_at }
      it { is_expected.not_to validate_presence_of :location }
      end
      end

      describe '.draft?' do
      context 'published' do
      before { allow(subject).to receive(:published?).and_return(true) }
      it 'is false' do
      expect(subject.draft?).to eq(false)
      end
      end
      context 'draft' do
      before { allow(subject).to receive(:published?).and_return(false) }
      it 'is true' do
      expect(subject.draft?).to eq(true)
      end
      end
      end

      describe '.has_duration_range?' do
      context 'with both values' do
      before { allow(subject).to receive(:starts_at).and_return(DateTime.now) }
      before { allow(subject).to receive(:ends_at).and_return(DateTime.tomorrow) }
      it 'is true' do
      expect(subject.has_duration_range?).to eq(true)
      end
      end
      context 'without both values' do
      before { allow(subject).to receive(:starts_at).and_return(nil) }
      before { allow(subject).to receive(:ends_at).and_return(nil) }
      it 'is false' do
      expect(subject.has_duration_range?).to eq(false)
      end
      end
      context 'without only one value' do
      before { allow(subject).to receive(:starts_at).and_return(DateTime.now) }
      before { allow(subject).to receive(:ends_at).and_return(nil) }
      it 'is false' do
      expect(subject.has_duration_range?).to eq(false)
      end
      end
      end

      describe '.duration_range_is_valid?' do
      context 'with valid range' do
      before { allow(subject).to receive(:has_duration_range?).and_return(true) }
      before { allow(subject).to receive(:starts_at).and_return(DateTime.now) }
      before { allow(subject).to receive(:ends_at).and_return(DateTime.tomorrow) }
      it 'is true' do
      expect(subject.duration_range_is_valid?).to eq(true)
      end
      end
      context 'without invalid range' do
      before { allow(subject).to receive(:has_duration_range?).and_return(true) }
      before { allow(subject).to receive(:starts_at).and_return(DateTime.tomorrow) }
      before { allow(subject).to receive(:ends_at).and_return(DateTime.now) }
      it 'is false' do
      expect(subject.duration_range_is_valid?).to eq(false)
      end
      end
      end

      describe '.set_duration' do
      it 'with valid range' do
      group_event = GroupEvent.create!(starts_at: DateTime.now, ends_at: DateTime.now + 10.days)
      expect(group_event.duration).to eq(10)
      end
      it 'with invalid range' do
      group_event = GroupEvent.create!(starts_at: DateTime.now, ends_at: DateTime.now - 10.days)
      expect(group_event.duration).to eq(nil)
      end
      end

      describe '.destroy' do
      it 'does not destroy the event' do
      group_event = GroupEvent.create!
      count = GroupEvent.all.count
      group_event.destroy
      expect(GroupEvent.unscoped.count).to eq(count)
      end
      it 'sets deleted' do
      group_event = GroupEvent.create!
      group_event.destroy
      expect(group_event.deleted).to eq(true)
      end
      end
      end









      share|improve this question









      New contributor




      Ibrah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I found myself working in this coding exercise for a work offer. I tried to apply TDD in the process but I didn't get any feedback from the company and I really would like to know what can be improved (pretty sure there are a lot of things). So any advice or refactor suggestion will be very much appreciated:




      Background:



      A group event will be created by an user. The group event should run for a whole number of days e.g.. 30 or 60. There should be attributes to set and update the start, end or duration of the event (and calculate the other value). The event also has a name, description (which supports formatting) and location. The event should be draft or published. To publish all of the fields are required, it can be saved with only a subset of fields before it’s published. When the event is deleted/remove it should be kept in the database and marked as such.



      Deliverable:



      Write an AR model, spec and migration for a GroupEvent that would meet the needs of the description above. Then write the api controller and spec to support JSON request/responses to manage these GroupEvents. For the purposes of this exercise, ignore auth.




      Here is my app/models/group_event.rb:



      class GroupEvent < ApplicationRecord
      validates :name, :description, :starts_at, :ends_at, :location, presence: true, if: :published?
      validate :duration_range_is_valid?, if: :published?
      before_save :set_duration, if: :duration_range_is_valid?

      def draft?
      !published?
      end

      def check_duration_range
      errors.add(:start_date, "Start date must be before end date") unless duration_range_is_valid?
      end

      def has_duration_range?
      starts_at.present? && ends_at.present?
      end

      def duration_range_is_valid?
      has_duration_range? and starts_at <= ends_at
      end

      def set_duration
      self.duration = ((self.ends_at - self.starts_at) / 1.day).to_i if duration_range_is_valid?
      end

      def destroy
      update_attribute(:deleted, true)
      end
      end


      Here my app/controllers/api/v1/group_events_controller.rb:



          class Api::V1::GroupEventsController < ApplicationController
      include Response
      include ExceptionHandler

      before_action :set_group_event, only: [:show, :update, :destroy]

      # GET /api/v1/group_events
      def index
      @group_events = GroupEvent.all
      json_response @group_events
      end

      # GET /api/v1/group_events/1
      def show
      json_response @group_event
      end

      # POST /api/v1/group_events
      def create
      @group_event = GroupEvent.create!(group_event_params)
      json_response(@group_event, :created)
      end

      # PATCH/PUT /api/v1/group_events/1
      def update
      if @group_event.update(group_event_params)
      json_response @group_event
      else
      json_response @group_event.errors, :unprocessable_entity
      end
      end

      # DELETE /api/v1/group_events/1
      def destroy
      @group_event.destroy
      end

      private
      def set_group_event
      @group_event = GroupEvent.find(params[:id])
      end

      def group_event_params
      params.permit(:name, :description, :deleted, :published, :location, :starts_at, :ends_at)
      end
      end


      My config/routes.rb:



      Rails.application.routes.draw do
      namespace :api do
      namespace :v1 do
      resources :group_events
      end
      end
      end


      And last but not least here is my spec/models/group_event_spec.rb:



      require 'rails_helper'

      RSpec.describe GroupEvent, type: :model do
      it 'has a valid factory' do
      expect(create(:group_event)).to be_valid
      end

      describe 'validations' do
      context 'published' do
      before { allow(subject).to receive(:published?).and_return(true) }
      it { is_expected.to validate_presence_of :name }
      it { is_expected.to validate_presence_of :description }
      it { is_expected.to validate_presence_of :starts_at }
      it { is_expected.to validate_presence_of :ends_at }
      it { is_expected.to validate_presence_of :location }
      end
      context 'draft' do
      before { allow(subject).to receive(:draft?).and_return(true) }
      it { is_expected.not_to validate_presence_of :name }
      it { is_expected.not_to validate_presence_of :description }
      it { is_expected.not_to validate_presence_of :starts_at }
      it { is_expected.not_to validate_presence_of :ends_at }
      it { is_expected.not_to validate_presence_of :location }
      end
      end

      describe '.draft?' do
      context 'published' do
      before { allow(subject).to receive(:published?).and_return(true) }
      it 'is false' do
      expect(subject.draft?).to eq(false)
      end
      end
      context 'draft' do
      before { allow(subject).to receive(:published?).and_return(false) }
      it 'is true' do
      expect(subject.draft?).to eq(true)
      end
      end
      end

      describe '.has_duration_range?' do
      context 'with both values' do
      before { allow(subject).to receive(:starts_at).and_return(DateTime.now) }
      before { allow(subject).to receive(:ends_at).and_return(DateTime.tomorrow) }
      it 'is true' do
      expect(subject.has_duration_range?).to eq(true)
      end
      end
      context 'without both values' do
      before { allow(subject).to receive(:starts_at).and_return(nil) }
      before { allow(subject).to receive(:ends_at).and_return(nil) }
      it 'is false' do
      expect(subject.has_duration_range?).to eq(false)
      end
      end
      context 'without only one value' do
      before { allow(subject).to receive(:starts_at).and_return(DateTime.now) }
      before { allow(subject).to receive(:ends_at).and_return(nil) }
      it 'is false' do
      expect(subject.has_duration_range?).to eq(false)
      end
      end
      end

      describe '.duration_range_is_valid?' do
      context 'with valid range' do
      before { allow(subject).to receive(:has_duration_range?).and_return(true) }
      before { allow(subject).to receive(:starts_at).and_return(DateTime.now) }
      before { allow(subject).to receive(:ends_at).and_return(DateTime.tomorrow) }
      it 'is true' do
      expect(subject.duration_range_is_valid?).to eq(true)
      end
      end
      context 'without invalid range' do
      before { allow(subject).to receive(:has_duration_range?).and_return(true) }
      before { allow(subject).to receive(:starts_at).and_return(DateTime.tomorrow) }
      before { allow(subject).to receive(:ends_at).and_return(DateTime.now) }
      it 'is false' do
      expect(subject.duration_range_is_valid?).to eq(false)
      end
      end
      end

      describe '.set_duration' do
      it 'with valid range' do
      group_event = GroupEvent.create!(starts_at: DateTime.now, ends_at: DateTime.now + 10.days)
      expect(group_event.duration).to eq(10)
      end
      it 'with invalid range' do
      group_event = GroupEvent.create!(starts_at: DateTime.now, ends_at: DateTime.now - 10.days)
      expect(group_event.duration).to eq(nil)
      end
      end

      describe '.destroy' do
      it 'does not destroy the event' do
      group_event = GroupEvent.create!
      count = GroupEvent.all.count
      group_event.destroy
      expect(GroupEvent.unscoped.count).to eq(count)
      end
      it 'sets deleted' do
      group_event = GroupEvent.create!
      group_event.destroy
      expect(group_event.deleted).to eq(true)
      end
      end
      end






      unit-testing ruby-on-rails api






      share|improve this question









      New contributor




      Ibrah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Ibrah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 8 hours ago









      Sᴀᴍ Onᴇᴌᴀ

      8,09961751




      8,09961751






      New contributor




      Ibrah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 12 hours ago









      Ibrah

      61




      61




      New contributor




      Ibrah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Ibrah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Ibrah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Ibrah is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209360%2ftdd-for-ruby-on-rails-api-for-group-event-items%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Ibrah is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Ibrah is a new contributor. Be nice, and check out our Code of Conduct.













          Ibrah is a new contributor. Be nice, and check out our Code of Conduct.












          Ibrah is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209360%2ftdd-for-ruby-on-rails-api-for-group-event-items%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Mont Emei

          Province de Neuquén

          Journaliste