我遇到了这个没有动作的控制器规范。对我来说很明显,它没有测试任何东西。但是对response.status == 200
的期望并没有失败。因此,我想了解Rspec如何构建控制器测试以及是否存在默认response.status == 200
。
describe 'SomeController', type: :controller do
let!(:user){ FactoryGirl.create :admin_user }
before { sign_in user }
describe 'GET#action' do
it 'response is success' do
expect(response.status).to eq 200
end
end
end
Finished in 0.93954 seconds (files took 1.63 seconds to load)
1 example, 0 failures
答案 0 :(得分:2)
作为RSpec controller spec documents point out:
控制器规范是Rails功能测试的RSpec包装器 (
ActionController::TestCase::Behavior
)。
因此,查看ActionController::TestCase::Behavior
下的ActionController::TestCase
的代码内文档,我们可以看到response
会自动提供Special Instance Variables section({{3} }),它是一个a @response
instance variable对象,表示最后一个HTTP响应的响应"。所以,这似乎可以解释为什么能够在不需要控制器规范中的明确请求的情况下访问200
,但为什么它的状态为ActionDispatch::TestResponse
?
好吧,> ActionDispatch::TestResponse.new
=> #<ActionDispatch::TestResponse:0x007fc449789b68
@blank=false,
@cache_control={},
@charset=nil,
@committed=false,
@content_type=nil,
@cv=
#<MonitorMixin::ConditionVariable:0x007fc449789848
@cond=#<Thread::ConditionVariable:0x007fc449789820>,
@monitor=#<ActionDispatch::TestResponse:0x007fc449789b68 ...>>,
@etag=nil,
@header={"X-Frame-Options"=>"SAMEORIGIN", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-Options"=>"nosniff"},
@mon_count=0,
@mon_mutex=#<Thread::Mutex:0x007fc449789a50>,
@mon_owner=nil,
@sending=false,
@sending_file=false,
@sent=false,
@status=200, # <<< Here's your default status.
@stream=#<ActionDispatch::Response::Buffer:0x007fc449789938 @buf=[], @closed=false, @response=#<ActionDispatch::TestResponse:0x007fc449789b68 ...>>>
继承自readable as just response
in the test,ActionDispatch::TestResponse
。您甚至可以在rails控制台中测试它:
response
所以,我希望这次深度探讨有助于您理解RSpec控制器规范中的# # Be sure to run `pod lib lint My Pod.podspec' to ensure this is a # valid spec before submitting. # # Any lines starting with a # are optional, but their use is encouraged # To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html # Pod::Spec.new do |s| s.name = "My Pod" s.version = "0.1.0" s.summary = "My Pod Stuff" # This description is used to generate tags and improve search results. # Think: What does it do? Why did you write it? What is the focus? # Try to keep it short, snappy and to the point. # Write the description between the DESC delimiters below. Finally, don't worry about the indent, CocoaPods strips it! s.description = <<-DESC Localize StoryBoard automatically. DESC s.homepage = "https://github.com/pod/MyPod.git" # s.screenshots = "www.example.com/screenshots_1", "www.example.com/screenshots_2" s.license = 'MIT' s.author = { } s.source = { :git => "https://github.com/controllerkit/MyPod.git", :tag => s.version.to_s } # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>' s.platform = :ios, '7.0' s.requires_arc = true s.source_files = 'Pod/Classes/**/*' s.resource_bundles = { 'My Pod' => ['Pod/Assets/*.png'] } # s.public_header_files = 'Pod/Classes/**/*.h' # s.frameworks = 'UIKit', 'MapKit' s.dependency 'CocoaLumberjack', '~> 2' s.dependency 'BlocksKit' end
对象,因为它确实是我的。