我对Rails 3.1应用程序进行了RSPEC集成测试,该应用程序需要通过发出带有JSON参数的POST请求和需要使用http_basic身份验证的移动头来测试移动客户端的API,因为请求对象在集成测试中不可用我有点卡住了
这是我到目前为止的代码
it "successfully posts scores" do
# request.env["HTTP_ACCEPT"] = "application/json" #This causes an error as request is nly available in controller tests
post "scores", :score => {:mobile_user_id => @mobile_user.id, :points => 50, :time_taken => 7275}.to_json,
:format => :json, :user_agent => 'Mobile', 'HTTP_AUTHORIZATION' => get_basic_auth
end
Run Code Online (Sandbox Code Playgroud)
发布请求无法识别我使用的是http基本身份验证,但不确定json的格式是否正确.任何帮助赞赏
get_basic_auth是一个帮助me4thod,看起来像这样
def get_basic_auth
user = 'user'
pw = 'secret'
ActionController::HttpAuthentication::Basic.encode_credentials user, pw
end
Run Code Online (Sandbox Code Playgroud)
我在我的控制器中使用了before_filter来检查看起来像这样的移动和http_basic_authentication
def authorize
logger.debug("@@@@ Authorizing request #{request.inspect}")
if mobile_device?
authenticate_or_request_with_http_basic do |username, password|
username == Mobile::Application.config.mobile_login_name && Mobile::Application.config.mobile_password
end …Run Code Online (Sandbox Code Playgroud) RSpec stub_model和mock_modelRSpec有什么区别?到目前为止,我知道存根用于防止实际方法被调用并返回预定义值,而模拟实际上是期望并且要求在接收器上调用该方法.
我也知道这些存根/模拟用于允许隔离测试,例如在控制器中而不触及模型.但我仍然对这两种方法感到困惑,究竟每种方法都使用了吗?非常感谢细节和例子.非常感谢!
我正在构建一个rails 4 app.我创建了一个支持文件来模拟登录.这是文件
module SpecTestHelper
def login(user)
request.session[:user_id] = user.id
end
def current_user
User.find(request.session[:user_id])
end
end
Run Code Online (Sandbox Code Playgroud)
config.include SpecTestHelper, :type => :controller
Run Code Online (Sandbox Code Playgroud)
describe BooksController, "user role" do
user = Fabricate(:user) do
role { Role.find_by_account_type("user") }
end
login(user)
end
Run Code Online (Sandbox Code Playgroud)
支持文件给出了未定义的方法错误.这是错误消息的一部分:
spec/controllers/books_controller_spec.rb:27:in `block in <top (required)>': undefined method `login' for #<Class:0x007f9f83193438> (NoMethodError)
Run Code Online (Sandbox Code Playgroud)
我正在测试CanCan.我知道测试CanCan的正确方法是测试能力,但已经完成了.
我正试图运行这个测试......
require 'spec_helper'
describe HomeController do
describe 'boilerplate routes' do
it "should route to '/about'" do
{ :get => "/about" }.should route_to(:controller => 'home', :action => 'show')
end
end
end
Run Code Online (Sandbox Code Playgroud)
...并且它始终失败并显示以下错误消息:
1) HomeController boilerplate routes should route to '/about'
Failure/Error: { :get => "/about" }.should route_to(:controller => 'home', :action => 'show')
NoMethodError:
undefined method `route_to' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x00000101499650>
# ./spec/routes/home_routes_spec.rb:8:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)
我rspec-rails 2.5.0和rails 3.0.5&一起使用rspec 2.5.1.
如何删除您创建的对象(在数据库和内存中)
有自动执行此操作的方法吗?
我有以下问题:
每个测试都将条目保存到数据库中.然后,下一个测试取决于这些条目.即使我想构建依赖于其他测试的测试,我也不能,因为测试执行的顺序是不可控的.
factories.rb:
sequence(:name) { |n| "purchaser #{n}" }
Run Code Online (Sandbox Code Playgroud)
organization_spec.rb:
context "when no supplier exists" do
it "finds no associated suppliers" do
purchaser = create(:organization_purchaser)
purchaser.partners.empty?.should == true
end
end
context "when one supplier exists" do
it "finds one associated suppliers" do
purchaser = create(:organization_purchaser)
supplier = create(:organization_supplier)
partnership = create(:partnership, organization: purchaser, partner: supplier)
purchaser.partners.last.name.should == "purchaser 1"
end
end
context "when two suppliers exist" do
it "finds two associated suppliers" do
purchaser = create(:organization_purchaser) …Run Code Online (Sandbox Code Playgroud) 我想在rspec测试中使用Mocks.
klass.any_instance.should_receive(:save).exactly(2).times.and_return(true)
Run Code Online (Sandbox Code Playgroud)
但我收到一条错误消息:
'消息"save"由<#Object>接收,但<#Object>已收到
临时我使用存根但准确性要使用模拟
我正在尝试测试未登录用户的行为如何
describe "not logged in user" do
user_no_rights
it "can't access action index" do
expect(get :index).to raise_error(CanCan::AccessDenied)
end
end
Run Code Online (Sandbox Code Playgroud)
我运行rspec时的输出
Failure/Error: expect(get :index).to raise_error("CanCan::AccessDenied:You are not authorized to access this page.")
CanCan::AccessDenied:
You are not authorized to access this page.
Run Code Online (Sandbox Code Playgroud)
所以它看起来像是正确的execption,但为什么规范不通过?
我正在尝试在模型中测试我的默认范围行.
我的测试如下:
it 'orders by ascending name by default' do
expect(Coaster.scoped.to_sql).to eq Coaster.order(:name).to_sql
end
Run Code Online (Sandbox Code Playgroud)
我的错误是:
expected: "SELECT \"coasters\".* FROM \"coasters\" ORDER BY name ASC, name"
got: "SELECT \"coasters\".* FROM \"coasters\" ORDER BY name ASC"
Run Code Online (Sandbox Code Playgroud)
, name错误第一行末尾的部分是什么意思,我该如何解决这个问题呢?
更新:
我的测试:
describe 'default scope' do
let!(:coaster_one) { FactoryGirl.create(:coaster, name: "Tower of Terror") }
let!(:coaster_two) { FactoryGirl.create(:coaster, name: "Apocalypse") }
it 'orders by ascending name' do
Coaster.all.should eq [:coaster_two, :coaster_one]
end
end
Run Code Online (Sandbox Code Playgroud)
我的错误:
expected: [:coaster_two, :coaster_one]
got: [#<Coaster id: 5, name: "Apocalypse", height: …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的控制器编写测试,以接收来自外部服务的请求.到目前为止,这是我的测试:
describe ApplyController do
context 'when valid' do
let(:parameters) do
file = File.join File.dirname(__FILE__), '..', 'samples', 'Indeed.json'
JSON.parse(File.read file)
end
let(:signature) { 'GC02UVj0d4bqa5peNFHdPQAZ2BI=' }
subject(:response) { post :indeed, parameters, 'X-Indeed-Signature' => signature }
it 'returns 200 ok if Request is valid' do
expect(response.status).to eq 200
end
end
end
Run Code Online (Sandbox Code Playgroud)
我的控制器现在看起来像这样:
class ApplyController < Application Controller
def indeed
binding.pry
end
end
Run Code Online (Sandbox Code Playgroud)
当我在测试中进入Pry并尝试检查request.headers['X-Indeed-Signature']我总是得到的价值nil
有什么东西我错过了吗?我正在使用Rails 3.2和Rspec 3
我运行我的rspec,大部分测试都失败了.我得到了同样的错误,这是:
Failure/Error: Unable to find matching line from backtrace
ActiveRecord::StatementInvalid:
PG::ConnectionBad: PQsocket() can't get socket descriptor: BEGIN
Run Code Online (Sandbox Code Playgroud)
我发现了一个问题,那就是类似我的问题,但目前还没有答案了,我也尝试从这个解决方案的链接,但它并没有对我做任何的差异.我打开了我的测试控制台并运行了一些最简单的查询,它运行起来.