{"id":1237,"date":"2025-03-24T08:52:31","date_gmt":"2025-03-23T23:52:31","guid":{"rendered":"https:\/\/dexall.co.jp\/articles\/?p=1237"},"modified":"2025-03-24T08:52:31","modified_gmt":"2025-03-23T23:52:31","slug":"rspec%e5%85%a5%e9%96%80%ef%bc%9asubject%e3%82%92%e4%bd%bf%e3%81%84%e3%81%93%e3%81%aa%e3%81%97%e3%81%a67%e5%80%8d%e9%80%9f%e3%81%8f%e3%83%86%e3%82%b9%e3%83%88%e3%82%92%e6%9b%b8%e3%81%8f%e5%ae%8c","status":"publish","type":"post","link":"https:\/\/dexall.co.jp\/articles\/?p=1237","title":{"rendered":"RSpec\u5165\u9580\uff1asubject\u3092\u4f7f\u3044\u3053\u306a\u3057\u30667\u500d\u901f\u304f\u30c6\u30b9\u30c8\u3092\u66f8\u304f\u5b8c\u5168\u30ac\u30a4\u30c9"},"content":{"rendered":"\n<div class=\"toc\"><br \/>\n<b>Warning<\/b>:  Undefined array key \"is_admin\" in <b>\/home\/xs392991\/dexall.co.jp\/public_html\/articles\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>116<\/b><br \/>\n<br \/>\n<b>Warning<\/b>:  Undefined array key \"is_category_top\" in <b>\/home\/xs392991\/dexall.co.jp\/public_html\/articles\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>121<\/b><br \/>\n<br \/>\n<b>Warning<\/b>:  Undefined array key \"is_top\" in <b>\/home\/xs392991\/dexall.co.jp\/public_html\/articles\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>128<\/b><br \/>\n    <div id=\"toc_container\" class=\"sgb-toc--bullets js-smooth-scroll\" data-dialog-title=\"\u76ee\u6b21\">\n      <p class=\"toc_title\">\u76ee\u6b21 <\/p>\n      <ul class=\"toc_list\">  <li class=\"first\">    <a href=\"#i-0\">RSpec subject\u3068\u306f\u4f55\u304b\uff1f\u305d\u306e\u5a01\u529b\u3092\u89e3\u8aac<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-1\">\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u91cd\u8907\u3092\u5287\u7684\u306b\u6e1b\u3089\u3059subject\u306e\u4ed5\u7d44\u307f<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-2\">\u5f93\u6765\u306elet\u3068\u306e\u9055\u3044\u3092\u7406\u89e3\u3059\u308b<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-3\">subject\u306e\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9\u30de\u30b9\u30bf\u30fc<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-4\">\u6697\u9ed9\u7684\u306asubject\u3067\u7c21\u6f54\u306b\u66f8\u304f<\/a>      <\/li>      <li>        <a href=\"#i-5\">\u660e\u793a\u7684\u306asubject\u3067\u610f\u56f3\u3092\u660e\u78ba\u306b<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-6\">is_expected\u3092\u7d44\u307f\u5408\u308f\u305b\u305f\u53ef\u8aad\u6027\u306e\u9ad8\u3044\u69cb\u6587<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-7\">\u5b9f\u8df5\u7684\u306asubject\u306e\u6d3b\u7528\u30d1\u30bf\u30fc\u30f3<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-8\">\u8907\u96d1\u306a\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u30c6\u30b9\u30c8\u3059\u308b\u969b\u306esubject\u6d3b\u7528\u6cd5<\/a>      <\/li>      <li>        <a href=\"#i-9\">\u5171\u6709\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3067\u306esubject\u306e\u4f7f\u3044\u65b9<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-10\">\u30cd\u30b9\u30c8\u3057\u305f\u63cf\u5199\u30d6\u30ed\u30c3\u30af\u3067\u306esubject\u7d99\u627f\u30c6\u30af\u30cb\u30c3\u30af<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-11\">subject\u3092\u4f7f\u3046\u969b\u306e\u6ce8\u610f\u70b9\u3068\u5bfe\u7b56<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-12\">\u30c6\u30b9\u30c8\u306e\u610f\u56f3\u304c\u4e0d\u660e\u78ba\u306b\u306a\u308b\u30ea\u30b9\u30af\u3092\u907f\u3051\u308b<\/a>      <\/li>      <li>        <a href=\"#i-13\">\u904e\u5ea6\u306a\u6697\u9ed9\u7684subject\u304c\u3082\u305f\u3089\u3059\u4fdd\u5b88\u6027\u4f4e\u4e0b<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-14\">subject\u547d\u540d\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-15\">\u30c1\u30fc\u30e0\u958b\u767a\u3067\u306esubject\u6d3b\u7528\u30ac\u30a4\u30c9\u30e9\u30a4\u30f3<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-16\">\u30b3\u30fc\u30c9\u30ec\u30d3\u30e5\u30fc\u3067\u6ce8\u76ee\u3059\u3079\u304dsubject\u306e\u30dd\u30a4\u30f3\u30c8<\/a>      <\/li>      <li>        <a href=\"#i-17\">\u30c1\u30fc\u30e0\u5185\u3067\u306esubject\u547d\u540d\u898f\u7d04\u306e\u7d71\u4e00<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-18\">\u65b0\u898f\u53c2\u753b\u30e1\u30f3\u30d0\u30fc\u3078\u306esubject\u8aac\u660e\u30a2\u30d7\u30ed\u30fc\u30c1<\/a>      <\/li>    <\/ul>  <\/li>  <li class=\"last\">    <a href=\"#i-19\">subject\u3092\u4f7f\u3063\u305f\u5b9f\u88c5\u4f8b\u96c6<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-20\">ActiveRecord\u30e2\u30c7\u30eb\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30c6\u30b9\u30c8<\/a>      <\/li>      <li>        <a href=\"#i-21\">API\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-22\">\u30b5\u30fc\u30d3\u30b9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30e6\u30cb\u30c3\u30c8\u30c6\u30b9\u30c8<\/a>      <\/li>    <\/ul>  <\/li><\/ul>\n      <a href=\"#\" class=\"sgb-toc-button js-toc-button\" rel=\"nofollow\" data-open-dialog=\"true\"><i class=\"fa fa-list\"><\/i><span class=\"sgb-toc-button__text\">\u76ee\u6b21\u3078<\/span><\/a>\n    <\/div><\/div><h2 class=\"wp-block-heading\" id=\"i-0\">RSpec subject\u3068\u306f\u4f55\u304b\uff1f\u305d\u306e\u5a01\u529b\u3092\u89e3\u8aac<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-1\">\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u91cd\u8907\u3092\u5287\u7684\u306b\u6e1b\u3089\u3059subject\u306e\u4ed5\u7d44\u307f<\/h3>\n\n\n\n<p>RSpec\u306esubject\u306f\u3001\u30c6\u30b9\u30c8\u5bfe\u8c61\u3068\u306a\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u5b9a\u7fa9\u3059\u308b\u5f37\u529b\u306a\u30e1\u30ab\u30cb\u30ba\u30e0\u3067\u3059\u3002\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u91cd\u8907\u3092\u6e1b\u3089\u3057\u3001\u3088\u308a\u7c21\u6f54\u3067\u4fdd\u5b88\u6027\u306e\u9ad8\u3044\u30c6\u30b9\u30c8\u3092\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u5f93\u6765\u306e\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u3067\u306f\u3001\u5404\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u3067\u540c\u3058\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u7e70\u308a\u8fd4\u3057\u5b9a\u7fa9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3057\u305f\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">describe User do\n  it \"is valid with correct attributes\" do\n    user = User.new(name: \"John\", email: \"john@example.com\")\n    expect(user).to be_valid\n  end\n\n  it \"has a correct full name\" do\n    user = User.new(name: \"John\", email: \"john@example.com\")\n    expect(user.full_name).to eq(\"John Doe\")\n  end\nend<\/pre>\n\n\n\n<p>subject\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30c6\u30b9\u30c8\u5bfe\u8c61\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4e00\u5ea6\u5b9a\u7fa9\u3059\u308b\u3060\u3051\u3067\u6e08\u307f\u307e\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">describe User do\n  subject { User.new(name: \"John\", email: \"john@example.com\") }\n\n  it \"is valid with correct attributes\" do\n    expect(subject).to be_valid\n  end\n\n  it \"has a correct full name\" do\n    expect(subject.full_name).to eq(\"John Doe\")\n  end\nend<\/pre>\n\n\n\n<p>\u3055\u3089\u306b\u3001<code>is_expected<\/code>\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u3088\u308a\u7c21\u6f54\u306b\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">describe User do\n  subject { User.new(name: \"John\", email: \"john@example.com\") }\n\n  it { is_expected.to be_valid }\n  it { is_expected.to have_attributes(full_name: \"John Doe\") }\nend<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-2\">\u5f93\u6765\u306elet\u3068\u306e\u9055\u3044\u3092\u7406\u89e3\u3059\u308b<\/h3>\n\n\n\n<p>subject\u3068let\u306f\u4e00\u898b\u4f3c\u3066\u3044\u308b\u3088\u3046\u306b\u898b\u3048\u307e\u3059\u304c\u3001\u91cd\u8981\u306a\u9055\u3044\u304c\u3042\u308a\u307e\u3059\uff1a<\/p>\n\n\n<div id=\"id-6100e981-db15-4af3-a72e-f5f2ac8090dd\">\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u5fb4<\/th><th>subject<\/th><th>let<\/th><\/tr><\/thead><tbody><tr><td>\u4e3b\u306a\u7528\u9014<\/td><td>\u30c6\u30b9\u30c8\u5bfe\u8c61\u306e\u5b9a\u7fa9<\/td><td>\u30c6\u30b9\u30c8\u306e\u88dc\u52a9\u30c7\u30fc\u30bf\u306e\u5b9a\u7fa9<\/td><\/tr><tr><td>\u6697\u9ed9\u7684\u306a\u4f7f\u7528<\/td><td>\u53ef\u80fd\uff08\u30af\u30e9\u30b9\u540d\u304b\u3089\u63a8\u6e2c\uff09<\/td><td>\u4e0d\u53ef\u80fd<\/td><\/tr><tr><td>is_expected\u3068\u306e\u76f8\u6027<\/td><td>\u5b8c\u74a7\u306a\u7d44\u307f\u5408\u308f\u305b<\/td><td>\u4f7f\u7528\u4e0d\u53ef<\/td><\/tr><tr><td>\u30e1\u30e2\u5316<\/td><td>\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u6709\u52b9<\/td><td>let\u3067\u306f\u660e\u793a\u7684\u306a\u6307\u5b9a\u304c\u5fc5\u8981<\/td><\/tr><\/tbody><\/table><\/figure>\n<\/div>\n\n\n<p>\u4ee5\u4e0b\u306f\u3001let\u3068subject\u3092\u4f75\u7528\u3059\u308b\u4e00\u822c\u7684\u306a\u30d1\u30bf\u30fc\u30f3\u3067\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">describe User do\n  let(:name) { \"John\" }\n  let(:email) { \"john@example.com\" }\n\n  subject { described_class.new(name: name, email: email) }\n\n  context \"with valid attributes\" do\n    it { is_expected.to be_valid }\n  end\n\n  context \"with invalid email\" do\n    let(:email) { \"invalid-email\" }\n    it { is_expected.not_to be_valid }\n  end\nend<\/pre>\n\n\n\n<p>\u3053\u306e\u3088\u3046\u306b\u3001subject\u306f\u30c6\u30b9\u30c8\u5bfe\u8c61\u3068\u306a\u308b\u4e3b\u8981\u306a\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u5b9a\u7fa9\u3057\u3001let\u306f\u305d\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u69cb\u7bc9\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u306a\u88dc\u52a9\u7684\u306a\u30c7\u30fc\u30bf\u3092\u5b9a\u7fa9\u3059\u308b\u3068\u3044\u3046\u5f79\u5272\u5206\u62c5\u304c\u660e\u78ba\u3067\u3059\u3002<\/p>\n\n\n\n<p>subject\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u5f97\u3089\u308c\u308b\u4e3b\u306a\u5229\u70b9\u306f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u30b3\u30fc\u30c9\u306e\u91cd\u8907\u524a\u6e1b<\/li>\n\n\n\n<li>\u30c6\u30b9\u30c8\u306e\u610f\u56f3\u306e\u660e\u78ba\u5316<\/li>\n\n\n\n<li>\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u4fdd\u5b88\u6027\u5411\u4e0a<\/li>\n\n\n\n<li><code>is_expected<\/code>\u69cb\u6587\u306b\u3088\u308b\u7c21\u6f54\u306a\u8a18\u8ff0<\/li>\n\n\n\n<li>\u6697\u9ed9\u7684\u306asubject\u306b\u3088\u308b\u67d4\u8edf\u6027<\/li>\n<\/ol>\n\n\n\n<p>\u3053\u308c\u3089\u306e\u5229\u70b9\u306b\u3088\u308a\u3001subject\u306f\u73fe\u4ee3\u306eRSpec\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306b\u304a\u3044\u3066\u4e0d\u53ef\u6b20\u306a\u30c4\u30fc\u30eb\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-3\">subject\u306e\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9\u30de\u30b9\u30bf\u30fc<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-4\">\u6697\u9ed9\u7684\u306asubject\u3067\u7c21\u6f54\u306b\u66f8\u304f<\/h3>\n\n\n\n<p>RSpec\u306b\u306f\u6697\u9ed9\u7684\u306asubject\u6a5f\u80fd\u304c\u3042\u308a\u3001\u30c6\u30b9\u30c8\u3059\u308b\u30af\u30e9\u30b9\u304b\u3089\u81ea\u52d5\u7684\u306bsubject\u3092\u63a8\u6e2c\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u3055\u3089\u306b\u30b3\u30fc\u30c9\u3092\u7c21\u6f54\u306b\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u30af\u30e9\u30b9\u306e\u5b9a\u7fa9\nclass Product\n  attr_reader :name, :price\n\n  def initialize(name:, price:)\n    @name = name\n    @price = price\n  end\n\n  def discounted?\n    price &lt; 1000\n  end\nend\n\n# \u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\ndescribe Product do\n  # \u6697\u9ed9\u7684\u306asubject\u306f described_class.new \u3068\u540c\u3058\n  subject { described_class.new(name: \"Test Product\", price: 500) }\n\n  # subject\u306f\u81ea\u52d5\u7684\u306bProduct\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3068\u3057\u3066\u6271\u308f\u308c\u308b\n  it { is_expected.to be_discounted }\n  it { is_expected.to have_attributes(name: \"Test Product\") }\nend<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-5\">\u660e\u793a\u7684\u306asubject\u3067\u610f\u56f3\u3092\u660e\u78ba\u306b<\/h3>\n\n\n\n<p>\u3088\u308a\u8907\u96d1\u306a\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u3067\u306f\u3001\u660e\u793a\u7684\u306asubject\u3092\u4f7f\u7528\u3057\u3066\u610f\u56f3\u3092\u660e\u78ba\u306b\u3067\u304d\u307e\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">describe Product do\n  # \u540d\u524d\u4ed8\u304dsubject\u3067\u610f\u56f3\u3092\u660e\u78ba\u306b\n  subject(:discounted_product) { \n    described_class.new(\n      name: \"Bargain Item\",\n      price: 500\n    )\n  }\n\n  subject(:premium_product) {\n    described_class.new(\n      name: \"Premium Item\",\n      price: 1500\n    )\n  }\n\n  # \u540d\u524d\u4ed8\u304dsubject\u3092\u53c2\u7167\u3057\u3066\u30c6\u30b9\u30c8\n  it \"correctly identifies discounted products\" do\n    expect(discounted_product).to be_discounted\n    expect(premium_product).not_to be_discounted\n  end\nend<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-6\">is_expected\u3092\u7d44\u307f\u5408\u308f\u305b\u305f\u53ef\u8aad\u6027\u306e\u9ad8\u3044\u69cb\u6587<\/h3>\n\n\n\n<p><code>is_expected<\/code>\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30c6\u30b9\u30c8\u3092\u3088\u308a\u81ea\u7136\u306a\u8a00\u8a9e\u306b\u8fd1\u3044\u5f62\u3067\u8a18\u8ff0\u3067\u304d\u307e\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">describe User do\n  context \"when user is an admin\" do\n    subject { \n      described_class.new(\n        name: \"Admin User\",\n        admin: true\n      )\n    }\n\n    it { is_expected.to be_admin }\n    it { is_expected.to be_able_to(:manage, :all) }\n    it { is_expected.to have_attributes(name: \"Admin User\") }\n  end\n\n  context \"with various permission checks\" do\n    subject(:user) { described_class.new(role: role) }\n\n    context \"as a regular user\" do\n      let(:role) { :user }\n      it { is_expected.not_to be_admin }\n      it { is_expected.to be_able_to(:read, :posts) }\n    end\n\n    context \"as a moderator\" do\n      let(:role) { :moderator }\n      it { is_expected.to be_able_to(:moderate, :posts) }\n    end\n  end\nend<\/pre>\n\n\n\n<p>\u4f7f\u7528\u4e0a\u306e\u30dd\u30a4\u30f3\u30c8\uff1a<\/p>\n\n\n<div id=\"id-d1883763-001c-4e99-91c8-93da27fc7864\">\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u4f7f\u7528\u30d1\u30bf\u30fc\u30f3<\/th><th>\u4f7f\u7528\u30b1\u30fc\u30b9<\/th><th>\u30e1\u30ea\u30c3\u30c8<\/th><\/tr><\/thead><tbody><tr><td>\u6697\u9ed9\u7684subject<\/td><td>\u30b7\u30f3\u30d7\u30eb\u306a\u30e2\u30c7\u30eb\u306e\u30c6\u30b9\u30c8<\/td><td>\u30b3\u30fc\u30c9\u306e\u7c21\u6f54\u3055<\/td><\/tr><tr><td>\u540d\u524d\u4ed8\u304dsubject<\/td><td>\u8907\u6570\u306e\u30c6\u30b9\u30c8\u5bfe\u8c61\u306e\u6bd4\u8f03<\/td><td>\u30c6\u30b9\u30c8\u306e\u610f\u56f3\u304c\u660e\u78ba<\/td><\/tr><tr><td>is_expected\u69cb\u6587<\/td><td>\u72b6\u614b\u3084\u5c5e\u6027\u306e\u691c\u8a3c<\/td><td>\u53ef\u8aad\u6027\u306e\u5411\u4e0a<\/td><\/tr><\/tbody><\/table><\/figure>\n<\/div>\n\n\n<p>subject\u3068is_expected\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u3088\u308a\u76f4\u611f\u7684\u306b\u8aad\u3081\u308b<\/li>\n\n\n\n<li>\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u304c\u5bb9\u6613\u306b\u306a\u308b<\/li>\n\n\n\n<li>\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u304c\u5206\u304b\u308a\u3084\u3059\u304f\u306a\u308b<\/li>\n\n\n\n<li>\u30b3\u30fc\u30c9\u306e\u91cd\u8907\u3092\u6700\u5c0f\u9650\u306b\u6291\u3048\u3089\u308c\u308b<\/li>\n<\/ol>\n\n\n\n<p>\u3053\u308c\u3089\u306e\u57fa\u672c\u7684\u306a\u30d1\u30bf\u30fc\u30f3\u3092\u62bc\u3055\u3048\u308b\u3053\u3068\u3067\u3001\u3088\u308a\u52b9\u7387\u7684\u306a\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u4f5c\u6210\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-7\">\u5b9f\u8df5\u7684\u306asubject\u306e\u6d3b\u7528\u30d1\u30bf\u30fc\u30f3<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-8\">\u8907\u96d1\u306a\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u30c6\u30b9\u30c8\u3059\u308b\u969b\u306esubject\u6d3b\u7528\u6cd5<\/h3>\n\n\n\n<p>\u8907\u96d1\u306a\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u30c6\u30b9\u30c8\u3059\u308b\u5834\u5408\u3001subject\u3092\u52b9\u679c\u7684\u306b\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u30c6\u30b9\u30c8\u306e\u53ef\u8aad\u6027\u3068\u4fdd\u5b88\u6027\u3092\u9ad8\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u306f\u3001\u6ce8\u6587\u51e6\u7406\u30b7\u30b9\u30c6\u30e0\u3092\u4f8b\u306b\u3057\u305f\u5b9f\u8df5\u7684\u306a\u4f7f\u7528\u30d1\u30bf\u30fc\u30f3\u3067\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Order\n  include ActiveModel::Model\n\n  attr_accessor :items, :user, :total, :status\n\n  def initialize(attributes = {})\n    super\n    @items ||= []\n    @status ||= 'pending'\n  end\n\n  def process!\n    return false if items.empty?\n    self.status = 'processed'\n    true\n  end\n\n  def total_price\n    items.sum(&amp;:price)\n  end\nend\n\nRSpec.describe Order do\n  # \u8907\u96d1\u306a\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u69cb\u7bc9\u3092\u30d8\u30eb\u30d1\u30fc\u30e1\u30bd\u30c3\u30c9\u306b\u30ab\u30d7\u30bb\u30eb\u5316\n  def build_order_with_items(item_count:, price_per_item:)\n    items = item_count.times.map do |i|\n      double(\"Item#{i}\", price: price_per_item)\n    end\n\n    described_class.new(\n      items: items,\n      user: double('User', premium?: true)\n    )\n  end\n\n  # \u7279\u5b9a\u306e\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u7528\u306b\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u3057\u305fsubject\n  subject(:order_with_items) { build_order_with_items(item_count: 3, price_per_item: 1000) }\n  subject(:empty_order) { described_class.new }\n\n  describe '#process!' do\n    context 'with items' do\n      subject { order_with_items }\n      it { is_expected.to be_truthy.on(:process!) }\n\n      it 'changes status to processed' do\n        subject.process!\n        expect(subject.status).to eq('processed')\n      end\n    end\n\n    context 'without items' do\n      subject { empty_order }\n      it { is_expected.to be_falsey.on(:process!) }\n    end\n  end\nend<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-9\">\u5171\u6709\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3067\u306esubject\u306e\u4f7f\u3044\u65b9<\/h3>\n\n\n\n<p>\u5171\u6709\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u3001subject\u3092\u52b9\u679c\u7684\u306b\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30c6\u30b9\u30c8\u306eDRY\u539f\u5247\u3092\u7dad\u6301\u3067\u304d\u307e\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">RSpec.shared_context 'with premium user' do\n  let(:user) { double('User', premium?: true, subscription: 'premium') }\n  subject(:premium_order) do\n    described_class.new(\n      user: user,\n      items: [double('Item', price: 1000)]\n    )\n  end\nend\n\nRSpec.describe Order do\n  describe 'premium user specific behavior' do\n    include_context 'with premium user'\n\n    it 'applies premium discount' do\n      expect(premium_order.total_price).to be &lt; 1000\n    end\n\n    it 'has premium shipping options' do\n      expect(premium_order.shipping_options).to include(:express)\n    end\n  end\nend<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-10\">\u30cd\u30b9\u30c8\u3057\u305f\u63cf\u5199\u30d6\u30ed\u30c3\u30af\u3067\u306esubject\u7d99\u627f\u30c6\u30af\u30cb\u30c3\u30af<\/h3>\n\n\n\n<p>\u30cd\u30b9\u30c8\u3057\u305f\u63cf\u5199\u30d6\u30ed\u30c3\u30af\u3067\u306f\u3001subject\u3092\u7d99\u627f\u3057\u306a\u304c\u3089\u5fc5\u8981\u306a\u90e8\u5206\u3060\u3051\u3092\u4e0a\u66f8\u304d\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">RSpec.describe ShoppingCart do\n  # \u57fa\u672c\u3068\u306a\u308bsubject\n  subject(:cart) do\n    described_class.new(\n      user: double('User'),\n      items: []\n    )\n  end\n\n  context 'when empty' do\n    it { is_expected.to be_empty }\n    it { is_expected.to have_attributes(total: 0) }\n  end\n\n  context 'with items' do\n    # \u57fa\u672c\u306ecart\u3092\u7d99\u627f\u3057\u3064\u3064\u3001items\u3060\u3051\u3092\u4e0a\u66f8\u304d\n    subject(:cart) do\n      super().tap do |cart|\n        cart.items = [\n          double('Item', price: 1000),\n          double('Item', price: 2000)\n        ]\n      end\n    end\n\n    it { is_expected.not_to be_empty }\n    it { is_expected.to have_attributes(total: 3000) }\n\n    context 'when applying discount' do\n      # \u3055\u3089\u306bdiscount\u3092\u8ffd\u52a0\n      subject(:cart) do\n        super().tap do |cart|\n          cart.apply_discount(percentage: 10)\n        end\n      end\n\n      it { is_expected.to have_attributes(total: 2700) }\n    end\n  end\nend<\/pre>\n\n\n\n<p>\u5b9f\u8df5\u7684\u306asubject\u306e\u6d3b\u7528\u306e\u30dd\u30a4\u30f3\u30c8\uff1a<\/p>\n\n\n<div id=\"id-d6d0c8be-f0e8-41d8-b985-7a0de1f9f151\">\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u30b7\u30ca\u30ea\u30aa<\/th><th>\u63a8\u5968\u30a2\u30d7\u30ed\u30fc\u30c1<\/th><th>\u5229\u70b9<\/th><\/tr><\/thead><tbody><tr><td>\u8907\u96d1\u306a\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u69cb\u7bc9<\/td><td>\u30d8\u30eb\u30d1\u30fc\u30e1\u30bd\u30c3\u30c9\u306e\u6d3b\u7528<\/td><td>\u518d\u5229\u7528\u6027\u306e\u5411\u4e0a\u3001\u30b3\u30fc\u30c9\u306e\u6574\u7406<\/td><\/tr><tr><td>\u5171\u6709\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8<\/td><td>shared_context\u3068subject\u306e\u7d44\u307f\u5408\u308f\u305b<\/td><td>\u30c6\u30b9\u30c8\u306e\u4e00\u8cab\u6027\u7dad\u6301<\/td><\/tr><tr><td>\u30cd\u30b9\u30c8\u3057\u305f\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8<\/td><td>super\u306b\u3088\u308b\u7d99\u627f\u3068\u90e8\u5206\u7684\u306a\u4e0a\u66f8\u304d<\/td><td>\u6bb5\u968e\u7684\u306a\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u306e\u69cb\u7bc9<\/td><\/tr><\/tbody><\/table><\/figure>\n<\/div>\n\n\n<p>\u3053\u308c\u3089\u306e\u30d1\u30bf\u30fc\u30f3\u3092\u9069\u5207\u306b\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u54c1\u8cea\u3068\u4fdd\u5b88\u6027\u3092\u5927\u304d\u304f\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-11\">subject\u3092\u4f7f\u3046\u969b\u306e\u6ce8\u610f\u70b9\u3068\u5bfe\u7b56<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-12\">\u30c6\u30b9\u30c8\u306e\u610f\u56f3\u304c\u4e0d\u660e\u78ba\u306b\u306a\u308b\u30ea\u30b9\u30af\u3092\u907f\u3051\u308b<\/h3>\n\n\n\n<p>subject\u306f\u5f37\u529b\u306a\u6a5f\u80fd\u3067\u3059\u304c\u3001\u4f7f\u3044\u65b9\u3092\u8aa4\u308b\u3068\u30c6\u30b9\u30c8\u306e\u610f\u56f3\u304c\u5206\u304b\u308a\u306b\u304f\u304f\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u554f\u984c\u306e\u3042\u308b\u30d1\u30bf\u30fc\u30f3\u3068\u3001\u305d\u306e\u6539\u5584\u65b9\u6cd5\u3092\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u554f\u984c\u306e\u3042\u308b\u30d1\u30bf\u30fc\u30f3\ndescribe User do\n  subject { described_class.new(params) }\n  let(:params) { { name: \"John\", role: role, status: status } }\n  let(:role) { :admin }\n  let(:status) { :active }\n\n  it { is_expected.to be_valid }  # \u30c6\u30b9\u30c8\u306e\u610f\u56f3\u304c\u4e0d\u660e\u78ba\nend\n\n# \u6539\u5584\u5f8c\u306e\u30d1\u30bf\u30fc\u30f3\ndescribe User do\n  subject(:admin_user) { described_class.new(params) }\n  let(:params) { { name: \"John\", role: :admin, status: :active } }\n\n  it \"is valid when created as an active admin\" do\n    expect(admin_user).to be_valid\n  end\nend<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-13\">\u904e\u5ea6\u306a\u6697\u9ed9\u7684subject\u304c\u3082\u305f\u3089\u3059\u4fdd\u5b88\u6027\u4f4e\u4e0b<\/h3>\n\n\n\n<p>\u6697\u9ed9\u7684\u306asubject\u306e\u904e\u5ea6\u306a\u4f7f\u7528\u306f\u3001\u30b3\u30fc\u30c9\u306e\u4fdd\u5b88\u6027\u3092\u4f4e\u4e0b\u3055\u305b\u308b\u539f\u56e0\u3068\u306a\u308a\u307e\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u907f\u3051\u308b\u3079\u304d\u30d1\u30bf\u30fc\u30f3\ndescribe ComplexService do\n  # \u6697\u9ed9\u7684\u306asubject\u306b\u4f9d\u5b58\u3057\u3059\u304e\u3066\u3044\u308b\n  it { is_expected.to respond_to(:process) }\n  it { is_expected.to respond_to(:validate) }\n\n  context \"when processing data\" do\n    before { subject.data = sample_data }\n    it { is_expected.to be_valid }\n  end\nend\n\n# \u63a8\u5968\u30d1\u30bf\u30fc\u30f3\ndescribe ComplexService do\n  subject(:service) { \n    described_class.new.tap do |s|\n      s.logger = logger\n      s.config = config\n    end\n  }\n  let(:logger) { instance_double(Logger) }\n  let(:config) { { timeout: 30 } }\n\n  context \"when processing data\" do\n    before { service.data = sample_data }\n\n    it \"validates the input data\" do\n      expect(service).to be_valid\n    end\n  end\nend<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-14\">subject\u547d\u540d\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/h3>\n\n\n\n<p>\u52b9\u679c\u7684\u306asubject\u547d\u540d\u306f\u3001\u30c6\u30b9\u30c8\u306e\u53ef\u8aad\u6027\u3068\u4fdd\u5b88\u6027\u3092\u9ad8\u3081\u307e\u3059\uff1a<\/p>\n\n\n<div id=\"id-db96c0ff-5c3c-4027-936e-473f8e210ef8\">\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u547d\u540d\u30d1\u30bf\u30fc\u30f3<\/th><th>\u4f7f\u7528\u4f8b<\/th><th>\u9069\u7528\u30b7\u30fc\u30f3<\/th><\/tr><\/thead><tbody><tr><td>\u5f79\u5272\u3092\u8868\u3059\u540d\u524d<\/td><td><code>admin_user<\/code>, <code>guest_user<\/code><\/td><td>\u30e6\u30fc\u30b6\u30fc\u306e\u6a29\u9650\u30c6\u30b9\u30c8<\/td><\/tr><tr><td>\u72b6\u614b\u3092\u8868\u3059\u540d\u524d<\/td><td><code>completed_order<\/code>, <code>pending_order<\/code><\/td><td>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u72b6\u614b\u30c6\u30b9\u30c8<\/td><\/tr><tr><td>\u632f\u308b\u821e\u3044\u3092\u8868\u3059\u540d\u524d<\/td><td><code>notifiable_event<\/code>, <code>processable_payment<\/code><\/td><td>\u7279\u5b9a\u306e\u6a5f\u80fd\u306e\u30c6\u30b9\u30c8<\/td><\/tr><\/tbody><\/table><\/figure>\n<\/div>\n\n\n<p>\u547d\u540d\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">RSpec.describe Order do\n  # \u826f\u3044\u4f8b\uff1a\u76ee\u7684\u304c\u660e\u78ba\u306a\u547d\u540d\n  subject(:completed_order) do\n    described_class.new(status: 'completed', completed_at: Time.current)\n  end\n\n  subject(:cancelled_order) do\n    described_class.new(status: 'cancelled', cancelled_at: Time.current)\n  end\n\n  # \u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u306b\u5fdc\u3058\u305f\u9069\u5207\u306asubject\u306e\u4f7f\u7528\n  context \"when order is completed\" do\n    it \"calculates the final price\" do\n      expect(completed_order.final_price).to eq(expected_price)\n    end\n  end\n\n  context \"when order is cancelled\" do\n    it \"applies cancellation fee\" do\n      expect(cancelled_order.cancellation_fee).to be_positive\n    end\n  end\nend<\/pre>\n\n\n\n<p>\u907f\u3051\u308b\u3079\u304d\u4e00\u822c\u7684\u306a\u554f\u984c\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u904e\u5ea6\u306b\u62bd\u8c61\u7684\u306asubject\u540d<\/li>\n\n\n\n<li>\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3068\u4e00\u81f4\u3057\u306a\u3044\u547d\u540d<\/li>\n\n\n\n<li>\u91cd\u8907\u3057\u305f\u5b9a\u7fa9<\/li>\n\n\n\n<li>\u904e\u5270\u306a\u4f9d\u5b58\u95a2\u4fc2<\/li>\n<\/ol>\n\n\n\n<p>\u5bfe\u7b56\u306e\u30dd\u30a4\u30f3\u30c8\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u660e\u78ba\u3067\u5177\u4f53\u7684\u306a\u547d\u540d\u3092\u5fc3\u304c\u3051\u308b<\/li>\n\n\n\n<li>\u5404\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u306e\u610f\u56f3\u3092\u660e\u78ba\u306b\u3059\u308b<\/li>\n\n\n\n<li>\u9069\u5207\u306a\u30b9\u30b3\u30fc\u30d7\u3067subject\u3092\u5b9a\u7fa9\u3059\u308b<\/li>\n\n\n\n<li>\u5fc5\u8981\u306b\u5fdc\u3058\u3066let\u3068\u7d44\u307f\u5408\u308f\u305b\u308b<\/li>\n\n\n\n<li>\u30c6\u30b9\u30c8\u306e\u53ef\u8aad\u6027\u3092\u512a\u5148\u3059\u308b<\/li>\n<\/ul>\n\n\n\n<p>\u3053\u308c\u3089\u306e\u6ce8\u610f\u70b9\u3092\u610f\u8b58\u3059\u308b\u3053\u3068\u3067\u3001\u3088\u308a\u4fdd\u5b88\u6027\u306e\u9ad8\u3044\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u3092\u4f5c\u6210\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-15\">\u30c1\u30fc\u30e0\u958b\u767a\u3067\u306esubject\u6d3b\u7528\u30ac\u30a4\u30c9\u30e9\u30a4\u30f3<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-16\">\u30b3\u30fc\u30c9\u30ec\u30d3\u30e5\u30fc\u3067\u6ce8\u76ee\u3059\u3079\u304dsubject\u306e\u30dd\u30a4\u30f3\u30c8<\/h3>\n\n\n\n<p>\u30c1\u30fc\u30e0\u958b\u767a\u306b\u304a\u3044\u3066\u3001subject\u306e\u4f7f\u7528\u306b\u95a2\u3059\u308b\u30b3\u30fc\u30c9\u30ec\u30d3\u30e5\u30fc\u306f\u975e\u5e38\u306b\u91cd\u8981\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u30c1\u30a7\u30c3\u30af\u30ea\u30b9\u30c8\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u52b9\u679c\u7684\u306a\u30ec\u30d3\u30e5\u30fc\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u30ec\u30d3\u30e5\u30fc\u6642\u306e\u30c1\u30a7\u30c3\u30af\u30dd\u30a4\u30f3\u30c8\u4f8b\ndescribe Product do\n  # \u2705 \u826f\u3044\u4f8b\uff1a\u76ee\u7684\u304c\u660e\u78ba\u3067\u3001\u5fc5\u8981\u306a\u6587\u8108\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u308b\n  subject(:discounted_product) do\n    described_class.new(\n      name: 'Sample Product',\n      price: 1000,\n      discount_rate: 0.2\n    )\n  end\n\n  # \u274c \u60aa\u3044\u4f8b\uff1a\u6587\u8108\u304c\u4e0d\u660e\u78ba\u3067\u3001\u4ed6\u306e\u958b\u767a\u8005\u304c\u7406\u89e3\u3057\u3065\u3089\u3044\n  subject { described_class.new(params) }\nend<\/pre>\n\n\n\n<p>\u30ec\u30d3\u30e5\u30fc\u6642\u306e\u4e3b\u8981\u30c1\u30a7\u30c3\u30af\u30dd\u30a4\u30f3\u30c8\uff1a<\/p>\n\n\n<div id=\"id-c702e468-df4a-4a8a-bbd8-9a003400d00e\">\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u89b3\u70b9<\/th><th>\u30c1\u30a7\u30c3\u30af\u5185\u5bb9<\/th><th>\u5177\u4f53\u4f8b<\/th><\/tr><\/thead><tbody><tr><td>\u547d\u540d<\/td><td>\u610f\u56f3\u304c\u660e\u78ba\u304b<\/td><td><code>valid_user<\/code> vs <code>user<\/code><\/td><\/tr><tr><td>\u30b9\u30b3\u30fc\u30d7<\/td><td>\u9069\u5207\u306a\u5834\u6240\u3067\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u304b<\/td><td>\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u5185\u3067\u306e\u5b9a\u7fa9<\/td><\/tr><tr><td>\u4f9d\u5b58\u95a2\u4fc2<\/td><td>\u904e\u5ea6\u306a\u4f9d\u5b58\u304c\u306a\u3044\u304b<\/td><td>Factory\/let\u306e\u9069\u5207\u306a\u4f7f\u7528<\/td><\/tr><tr><td>\u53ef\u8aad\u6027<\/td><td>\u30c6\u30b9\u30c8\u306e\u610f\u56f3\u304c\u660e\u78ba\u304b<\/td><td>\u660e\u78ba\u306a\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u8a18\u8ff0<\/td><\/tr><\/tbody><\/table><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"i-17\">\u30c1\u30fc\u30e0\u5185\u3067\u306esubject\u547d\u540d\u898f\u7d04\u306e\u7d71\u4e00<\/h3>\n\n\n\n<p>\u30c1\u30fc\u30e0\u5185\u3067\u4e00\u8cab\u6027\u306e\u3042\u308b\u547d\u540d\u898f\u7d04\u3092\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u3067\u3001\u30b3\u30fc\u30c9\u306e\u53ef\u8aad\u6027\u3068\u4fdd\u5b88\u6027\u304c\u5411\u4e0a\u3057\u307e\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">RSpec.describe Order do\n  # \u547d\u540d\u898f\u7d04\u4f8b\n  context 'status based naming' do\n    subject(:pending_order) { create_order(status: :pending) }\n    subject(:completed_order) { create_order(status: :completed) }\n  end\n\n  context 'behavior based naming' do\n    subject(:cancelable_order) { create_order(status: :pending) }\n    subject(:refundable_order) { create_order(paid: true) }\n  end\n\n  context 'state based naming' do\n    subject(:paid_order) { create_order(paid: true) }\n    subject(:unpaid_order) { create_order(paid: false) }\n  end\n\n  private\n\n  def create_order(attributes = {})\n    described_class.new(default_attributes.merge(attributes))\n  end\nend<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-18\">\u65b0\u898f\u53c2\u753b\u30e1\u30f3\u30d0\u30fc\u3078\u306esubject\u8aac\u660e\u30a2\u30d7\u30ed\u30fc\u30c1<\/h3>\n\n\n\n<p>\u65b0\u3057\u3044\u30c1\u30fc\u30e0\u30e1\u30f3\u30d0\u30fc\u304csubject\u3092\u52b9\u679c\u7684\u306b\u4f7f\u7528\u3067\u304d\u308b\u3088\u3046\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u6bb5\u968e\u7684\u306a\u5b66\u7fd2\u30a2\u30d7\u30ed\u30fc\u30c1\u3092\u63a8\u5968\u3057\u307e\u3059\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u57fa\u672c\u7684\u306a\u4f7f\u7528\u65b9\u6cd5\u306e\u8aac\u660e<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Step 1: \u57fa\u672c\u7684\u306asubject\u306e\u4f7f\u7528\ndescribe User do\n  subject { described_class.new(name: 'John') }\n  it { is_expected.to be_valid }\nend\n\n# Step 2: \u540d\u524d\u4ed8\u304dsubject\u306e\u5c0e\u5165\ndescribe User do\n  subject(:admin_user) { described_class.new(role: :admin) }\n  it 'has admin privileges' do\n    expect(admin_user).to be_admin\n  end\nend\n\n# Step 3: \u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3067\u306e\u4f7f\u7528\ndescribe User do\n  context 'with admin role' do\n    subject(:user) { described_class.new(role: :admin) }\n    it { is_expected.to be_able_to(:manage, :all) }\n  end\nend<\/pre>\n\n\n\n<p>\u30c1\u30fc\u30e0\u958b\u767a\u3067\u306e\u6210\u529f\u306e\u30dd\u30a4\u30f3\u30c8\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u660e\u78ba\u306a\u30ac\u30a4\u30c9\u30e9\u30a4\u30f3\u306e\u63d0\u4f9b<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>subject\u4f7f\u7528\u306e\u57fa\u6e96<\/li>\n\n\n\n<li>\u547d\u540d\u898f\u5247\u306e\u6587\u66f8\u5316<\/li>\n\n\n\n<li>\u30ec\u30d3\u30e5\u30fc\u57fa\u6e96\u306e\u660e\u78ba\u5316<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u52b9\u679c\u7684\u306a\u30ca\u30ec\u30c3\u30b8\u5171\u6709<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306e\u63d0\u4f9b<\/li>\n\n\n\n<li>\u3088\u304f\u3042\u308b\u9593\u9055\u3044\u306e\u5171\u6709<\/li>\n\n\n\n<li>\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u306e\u6587\u66f8\u5316<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u7d99\u7d9a\u7684\u306a\u6539\u5584<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b9a\u671f\u7684\u306a\u30ac\u30a4\u30c9\u30e9\u30a4\u30f3\u306e\u898b\u76f4\u3057<\/li>\n\n\n\n<li>\u30d5\u30a3\u30fc\u30c9\u30d0\u30c3\u30af\u306e\u53ce\u96c6\u3068\u53cd\u6620<\/li>\n\n\n\n<li>\u65b0\u3057\u3044\u30d1\u30bf\u30fc\u30f3\u306e\u5171\u6709<\/li>\n<\/ul>\n\n\n\n<p>\u3053\u308c\u3089\u306e\u30ac\u30a4\u30c9\u30e9\u30a4\u30f3\u3092\u9069\u5207\u306b\u904b\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30c1\u30fc\u30e0\u5168\u4f53\u306e\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u54c1\u8cea\u5411\u4e0a\u304c\u671f\u5f85\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-19\">subject\u3092\u4f7f\u3063\u305f\u5b9f\u88c5\u4f8b\u96c6<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-20\">ActiveRecord\u30e2\u30c7\u30eb\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30c6\u30b9\u30c8<\/h3>\n\n\n\n<p>ActiveRecord\u30e2\u30c7\u30eb\u306e\u30c6\u30b9\u30c8\u3067\u306f\u3001subject\u3092\u52b9\u679c\u7684\u306b\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u69d8\u3005\u306a\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30d1\u30bf\u30fc\u30f3\u3092\u7c21\u6f54\u306b\u8a18\u8ff0\u3067\u304d\u307e\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Product &lt; ApplicationRecord\n  validates :name, presence: true\n  validates :price, numericality: { greater_than: 0 }\n  validates :sku, format: { with: \/\\A[A-Z]{2}\\d{6}\\z\/ }\n\n  belongs_to :category\n  has_many :order_items\n\n  def in_stock?\n    stock_count.positive?\n  end\nend\n\nRSpec.describe Product do\n  subject(:product) do\n    described_class.new(\n      name: \"Sample Product\",\n      price: 1000,\n      sku: \"AB123456\",\n      stock_count: stock_count\n    )\n  end\n\n  let(:stock_count) { 5 }\n\n  # \u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u306e\u30c6\u30b9\u30c8\n  context 'validations' do\n    it { is_expected.to validate_presence_of(:name) }\n    it { is_expected.to validate_numericality_of(:price).is_greater_than(0) }\n    it { is_expected.to allow_value(\"AB123456\").for(:sku) }\n    it { is_expected.not_to allow_value(\"123456\").for(:sku) }\n  end\n\n  # \u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u306e\u30c6\u30b9\u30c8\n  context 'associations' do\n    it { is_expected.to belong_to(:category) }\n    it { is_expected.to have_many(:order_items) }\n  end\n\n  # \u30ab\u30b9\u30bf\u30e0\u30e1\u30bd\u30c3\u30c9\u306e\u30c6\u30b9\u30c8\n  describe '#in_stock?' do\n    context 'when stock exists' do\n      let(:stock_count) { 1 }\n      it { is_expected.to be_in_stock }\n    end\n\n    context 'when out of stock' do\n      let(:stock_count) { 0 }\n      it { is_expected.not_to be_in_stock }\n    end\n  end\nend<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-21\">API\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9<\/h3>\n\n\n\n<p>API\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u30c6\u30b9\u30c8\u3059\u308b\u969b\u3082\u3001subject\u3092\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u898b\u901a\u3057\u306e\u826f\u3044\u30c6\u30b9\u30c8\u3092\u66f8\u3051\u307e\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class API::ProductsController &lt; ApplicationController\n  def show\n    @product = Product.find(params[:id])\n    render json: ProductSerializer.new(@product)\n  end\nend\n\nRSpec.describe API::ProductsController do\n  describe 'GET #show' do\n    subject(:make_request) { get :show, params: { id: product.id } }\n\n    let(:product) do\n      create(:product,\n        name: \"Test Product\",\n        price: 1000,\n        description: \"Awesome product\"\n      )\n    end\n\n    context 'when product exists' do\n      before { make_request }\n\n      it 'returns success status' do\n        expect(response).to have_http_status(:success)\n      end\n\n      it 'returns correct product data' do\n        expect(json_response).to include(\n          'name' =&gt; product.name,\n          'price' =&gt; product.price,\n          'description' =&gt; product.description\n        )\n      end\n    end\n\n    context 'when product does not exist' do\n      let(:product) { build_stubbed(:product) }\n\n      it 'returns not found status' do\n        expect { make_request }.to raise_error(ActiveRecord::RecordNotFound)\n      end\n    end\n  end\nend<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-22\">\u30b5\u30fc\u30d3\u30b9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30e6\u30cb\u30c3\u30c8\u30c6\u30b9\u30c8<\/h3>\n\n\n\n<p>\u30b5\u30fc\u30d3\u30b9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30c6\u30b9\u30c8\u3067\u306f\u3001\u8907\u96d1\u306a\u632f\u308b\u821e\u3044\u3092subject\u3092\u4f7f\u3063\u3066\u52b9\u679c\u7684\u306b\u30c6\u30b9\u30c8\u3067\u304d\u307e\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class OrderProcessor\n  def initialize(order, payment_gateway)\n    @order = order\n    @payment_gateway = payment_gateway\n  end\n\n  def process\n    return false unless @order.valid?\n    return false unless process_payment\n\n    @order.update(status: 'completed')\n    OrderMailer.confirmation_email(@order).deliver_later\n    true\n  end\n\n  private\n\n  def process_payment\n    @payment_gateway.charge(@order.total_amount)\n  end\nend\n\nRSpec.describe OrderProcessor do\n  subject(:processor) do\n    described_class.new(order, payment_gateway)\n  end\n\n  let(:order) do\n    instance_double(Order,\n      valid?: order_valid?,\n      total_amount: 1000,\n      update: true\n    )\n  end\n\n  let(:payment_gateway) do\n    instance_double(PaymentGateway,\n      charge: payment_successful?\n    )\n  end\n\n  let(:order_valid?) { true }\n  let(:payment_successful?) { true }\n\n  describe '#process' do\n    context 'when everything succeeds' do\n      it 'processes the order successfully' do\n        expect(processor.process).to be true\n      end\n\n      it 'updates the order status' do\n        processor.process\n        expect(order).to have_received(:update).with(status: 'completed')\n      end\n    end\n\n    context 'when order is invalid' do\n      let(:order_valid?) { false }\n\n      it 'fails to process the order' do\n        expect(processor.process).to be false\n      end\n\n      it 'does not process payment' do\n        processor.process\n        expect(payment_gateway).not_to have_received(:charge)\n      end\n    end\n\n    context 'when payment fails' do\n      let(:payment_successful?) { false }\n\n      it 'fails to process the order' do\n        expect(processor.process).to be false\n      end\n\n      it 'does not update the order status' do\n        processor.process\n        expect(order).not_to have_received(:update)\n      end\n    end\n  end\nend<\/pre>\n\n\n\n<p>\u3053\u308c\u3089\u306e\u5b9f\u88c5\u4f8b\u304b\u3089\u5f97\u3089\u308c\u308b\u4e3b\u306a\u30dd\u30a4\u30f3\u30c8\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u30c6\u30b9\u30c8\u306e\u7a2e\u985e\u306b\u5fdc\u3058\u305f\u9069\u5207\u306asubject\u306e\u4f7f\u3044\u5206\u3051<\/li>\n\n\n\n<li>\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3054\u3068\u306e\u660e\u78ba\u306a\u72b6\u614b\u8a2d\u5b9a<\/li>\n\n\n\n<li>\u30e2\u30c3\u30af\u3084\u30b9\u30bf\u30d6\u3068\u306e\u52b9\u679c\u7684\u306a\u7d44\u307f\u5408\u308f\u305b<\/li>\n\n\n\n<li>\u53ef\u8aad\u6027\u306e\u9ad8\u3044\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u306e\u69cb\u7bc9<\/li>\n<\/ol>\n\n\n\n<p>\u3053\u308c\u3089\u306e\u4f8b\u3092\u53c2\u8003\u306b\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u7279\u6027\u306b\u5408\u308f\u305b\u3066subject\u3092\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u4fdd\u5b88\u6027\u306e\u9ad8\u3044\u52b9\u679c\u7684\u306a\u30c6\u30b9\u30c8\u3092\u4f5c\u6210\u3067\u304d\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Warning: Undefined array key &#8220;is_admin&#8221; in \/home\/xs392991\/dexall.co.jp\/public_html\/articles\/wp-content\/themes\/ &#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":{"0":"post-1237","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-ruby","7":"nothumb"},"_links":{"self":[{"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts\/1237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1237"}],"version-history":[{"count":1,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts\/1237\/revisions"}],"predecessor-version":[{"id":1238,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts\/1237\/revisions\/1238"}],"wp:attachment":[{"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}