{"id":9781,"date":"2020-03-21T07:13:30","date_gmt":"2020-03-20T22:13:30","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=9781"},"modified":"2023-05-27T22:27:22","modified_gmt":"2023-05-27T13:27:22","slug":"%e3%80%90rspec%e3%80%91%e3%80%8cshoulda-matchers%e3%80%8d%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=9781","title":{"rendered":"\u3010RSpec\u3011\u300cShoulda Matchers\u300d\u306b\u3064\u3044\u3066"},"content":{"rendered":"<h2>Shoulda Matchers\u3068\u306f\uff1f<\/h2>\n<p>Shoulda Matchers\u306fActiveModel\u3084ActiveRecord\u7b49\u3067\u4f7f\u3048\u308b\u8c4a\u5bcc\u306a\u30de\u30c3\u30c1\u30e3\u306e\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306b\u306a\u308a\u307e\u3059\u3002\uff08\u3059\u3067\u306b\u958b\u767a\u306f\u505c\u6b62\u3055\u308c\u3066\u304a\u308a\u307e\u3059\u3002\uff09<\/p>\n<p>\u8907\u96d1\u306b\u306a\u308a\u3084\u3059\u3044Rais\u306e\u30c6\u30b9\u30c8\u30921\u884c(\u30ef\u30f3\u30e9\u30a4\u30ca\u30fc)\u3067\u8a18\u8ff0\u3059\u308b\u3053\u3068\u3092\u76ee\u7684\u3068\u3057\u3066\u958b\u767a\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001\u30e2\u30c7\u30eb\u3060\u3051\u3067\u306a\u304f\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u306e\u30c6\u30b9\u30c8\u3092\u652f\u63f4\u3059\u308b\u6a5f\u80fd\u3082\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<h2>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n<p>Gemfile\u306b\u4e0b\u8a18\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">group :development, :test do\r\n  gem 'shoulda-matchers',\r\n    git: 'https:\/\/github.com\/thoughtbot\/shoulda-matchers.git',\r\n    branch: 'rails-5'\r\nend<\/pre>\n<p>spec\/rails_helper.rb\u306e\u672b\u5c3e\u306b\u4e0b\u8a18\u306e\u8a18\u8ff0\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">Shoulda::Matchers.configure do |config|\r\n  config.integrate do |with|\r\n    with.test_framework :rspec\r\n    with.library :rails\r\n  end\r\nend<\/pre>\n<h2>\u69cb\u6587<\/h2>\n<h3>\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u306e\u30c6\u30b9\u30c8<\/h3>\n<h4>\u30b9\u30c6\u30fc\u30bf\u30b9\u306e\u30c6\u30b9\u30c8<\/h4>\n<pre class=\"lang:default decode:true\">is_expected.to respond_with(:success)<\/pre>\n<h4>\u30ec\u30f3\u30c0\u30fc\u3059\u308b\u30c6\u30b9\u30c8<\/h4>\n<p>\u4e0b\u8a18\u306e\u5834\u5408\u306f\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u306e\u30a2\u30af\u30b7\u30e7\u30f3\u304c\u3061\u3083\u3093\u3068new\u3067\u30ec\u30f3\u30c0\u30fc\u3055\u308c\u3066\u3044\u308b\u304b\u30c1\u30a7\u30c3\u30af\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">is_expected.to render_template(:new)<\/pre>\n<h3>\u30e2\u30c7\u30eb\u306e\u30c6\u30b9\u30c8<\/h3>\n<p>\u901a\u5e38\u306f\u8907\u6570\u884c\u304b\u304b\u3063\u3066\u3057\u307e\u3044\u304c\u3061\u306b\u306a\u308b\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3084\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u306e\u30c6\u30b9\u30c8\u30921\u884c\u3067\u66f8\u3051\u308b\u306e\u3067\u975e\u5e38\u306b\u4fbf\u5229\u3067\u3059\u3002<\/p>\n<h4>validate_presence_of :\u30d5\u30a3\u30fc\u30eb\u30c9\u540d<\/h4>\n<p>\u5bfe\u8c61\u306e\u30e2\u30c7\u30eb\u306e\u4e2d\u306b\u6307\u5b9a\u3057\u305f\u30d5\u30a3\u30fc\u30eb\u30c9\u540d\u304c\u5b58\u5728\u3057\u3066\u3044\u308b\u304b\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\"> it { is_expected.to validate_presence_of :name }<\/pre>\n<h4>validate_uniqueness_of(:\u30d5\u30a3\u30fc\u30eb\u30c9\u540d)<\/h4>\n<p>\u5bfe\u8c61\u306e\u30e2\u30c7\u30eb\u306e\u4e2d\u306b\u6307\u5b9a\u3057\u305f\u30d5\u30a3\u30fc\u30eb\u30c9\u304c\u4e00\u610f\u3067\u5b58\u5728\u3057\u3066\u3044\u308b\u304b\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">it { is_expected.to validate_uniqueness_of(:name) }<\/pre>\n<h3>have_many(:\u95a2\u9023\u30e2\u30c7\u30eb)<\/h3>\n<p>1:\u591a\u306e\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u306e\u30c6\u30b9\u30c8\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">it { is_expected.to have_many(:users) }<\/pre>\n","protected":false},"excerpt":{"rendered":"Shoulda Matchers\u3068\u306f\uff1f Shoulda Matchers\u306fActiveModel\u3084ActiveRecord\u7b49\u3067\u4f7f\u3048\u308b\u8c4a\u5bcc\u306a\u30de\u30c3\u30c1\u30e3\u306e\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306b\u306a\u308a\u307e\u3059\u3002\uff08\u3059\u3067\u306b\u958b\u767a\u306f\u505c\u6b62\u3055\u308c\u3066\u304a\u308a\u307e\u3059\u3002\uff09 \u8907\u96d1\u306b\u306a\u308a [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[82],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/9781"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9781"}],"version-history":[{"count":5,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/9781\/revisions"}],"predecessor-version":[{"id":20455,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/9781\/revisions\/20455"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9781"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}