How to Test a Concern in Rails

后端 未结 4 1074
[愿得一人]
[愿得一人] 2020-12-07 10:36

Given that I have a Personable concern in my Rails 4 application which has a full_name method, how would I go about testing this using RSpec?

4条回答
  •  孤城傲影
    2020-12-07 11:24

    In response to the comments I've received, here's what I've ended up doing (if anyone has improvements please feel free to post them):

    spec/concerns/personable_spec.rb

    require 'spec_helper'
    
    describe Personable do
      let(:test_class) { Struct.new(:first_name, :last_name) { include Personable } }
      let(:personable) { test_class.new("Stewart", "Home") }
    
      it "has a full_name" do
        expect(personable.full_name).to eq("#{personable.first_name} #{personable.last_name}")
      end
    end
    

提交回复
热议问题