在我的ApplicationController中,我有一个定义为辅助方法的方法:
helper_method :some_method_here
我正在使用Rails3和RSpec2
为了确保我的应用程序不容易受到这种攻击,我试图在RSpec中创建一个控制器测试来覆盖它.为了做到这一点,我需要能够发布原始JSON,但我似乎没有找到一种方法来做到这一点.在进行一些研究时,我已经确定使用RAW_POST_DATA标题至少有一种方法可以这样做,但这似乎不再起作用了:
it "should not be exploitable by using an integer token value" do
request.env["CONTENT_TYPE"] = "application/json"
request.env["RAW_POST_DATA"] = { token: 0 }.to_json
post :reset_password
end
Run Code Online (Sandbox Code Playgroud)
当我查看params散列时,令牌根本没有设置,它只包含{ "controller" => "user", "action" => "reset_password" }.我在尝试使用XML时得到了相同的结果,或者甚至在尝试使用常规的帖子数据时,在所有情况下,它似乎都没有设置它的周期.
我知道,随着最近的Rails漏洞,参数被散列的方式发生了变化,但仍有办法通过RSpec发布原始数据吗?我可以以某种方式直接使用Rack::Test::Methods?
使用expectrspec-2.11中的新语法,如何使用隐式subject?有没有比明确引用更好的方法subject,如下所示?
describe User do
it 'is valid' do
expect(subject).to be_valid # <<< can `subject` be implicit?
end
end
Run Code Online (Sandbox Code Playgroud) 我正在尝试为此声明构建一个规范.'puts'很容易
print "'#{@file}' doesn't exist: Create Empty File (y/n)?"
Run Code Online (Sandbox Code Playgroud) 解
感谢史蒂文哈曼的这个要点,我得到了它的工作.devise_mail_helpers.rb
module Features
module MailHelpers
def last_email
ActionMailer::Base.deliveries[0]
end
# Can be used like:
# extract_token_from_email(:reset_password)
def extract_token_from_email(token_name)
mail_body = last_email.body.to_s
mail_body[/#{token_name.to_s}_token=([^"]+)/, 1]
end
end
end
Run Code Online (Sandbox Code Playgroud)
我将文件添加devise_mail_helpers.rb到功能规格所在的文件夹中并编写了此规范.
require 'devise_mail_helpers.rb'
include Features
include MailHelpers
describe "PasswordResets" do
it "emails user when requesting password reset" do
user = FactoryGirl.create(:user)
visit root_url
find("#login_link").click
click_link "Forgot your password?"
fill_in "Email", :with => user.email
click_button "Send instructions"
current_path.should eq('/users/sign_in')
page.should have_content("You will receive an email with instructions about how to …Run Code Online (Sandbox Code Playgroud) 我试图包括一些助手用rspec测试,但没有运气.
我做了什么:
support/helpers.rb在我的spec文件夹下创建了一个文件
支持/ helpers.rb
module Helpers
include ActionView::Helpers::NumberHelper
include ActionView::Helpers::TextHelper
end
Run Code Online (Sandbox Code Playgroud)
并试图要求这个文件spec_helper.rb.
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'rubygems'
require 'spork'
require 'support/helpers'
Spork.prefork do
.
.
end
Run Code Online (Sandbox Code Playgroud)
这会生成以下错误:
/spec/support/helpers.rb:2:in `<module:Helpers>': uninitialized constant Helpers::ActionView (NameError)
Run Code Online (Sandbox Code Playgroud)
我应该如何使用Rspec提供这些助手?
谢谢.
我已经阅读了RSpec手册中关于差异的内容,但有些事情仍然令人困惑.其他所有来源,包括"RSpec Book"仅解释"let",而"The Rails 3 Way"与手册一样令人困惑.
我理解"let"仅在调用时进行评估,并在范围内保持相同的值.所以有意义的是,在手册的第一个例子中,第一个测试通过,因为"let"只调用一次,第二个测试通过,因为它增加了第一个测试的值(在第一个测试中评估了一次)并具有1)的值.
之后,因为"让!" 在定义时进行评估,并在被调用时再次进行,如果测试没有失败,因为"count.should eq(1)"应该是"count.should eq(2)"?
任何帮助,将不胜感激.
关于Relish的前后钩子文档仅显示before(:suite)之前调用的before(:all).
我什么时候应该使用另一个?
我想知道before(如下所示)是否与before :allRSpec 相同.有时既:each没有:all指定也没有指定,它使我对before实际做的事感到困惑.
require 'spec_helper'
describe "this is a description" do
before do # vs. before :all or before :each
# do something...
end
end
Run Code Online (Sandbox Code Playgroud)
如果有人可以解释差异,如果有的话,将不胜感激.谢谢!
我想知道如何在rspec中测试find_each调用.我习惯于简单地存在我希望模型返回的内容,所以我不依赖于db中的测试数据,如下所示:
MyClass.stub(:find).and_return(my_mock)
Run Code Online (Sandbox Code Playgroud)
但是,在另一个班级我这样做:
MyClass.find_each do |instance_of_my_class|
do_stuff_here_on(instance_of_my_class)
end
Run Code Online (Sandbox Code Playgroud)
我发现如果我这样做:
MyClass.stub(:find_each).and_return([one_mock, two_mock])
Run Code Online (Sandbox Code Playgroud)
在规范测试中,"在这里做东西"部分没有被执行.有谁知道如何存储find_each进行rspec测试?
rspec2 ×10
rspec ×8
ruby ×3
rspec-rails ×2
devise ×1
factory-bot ×1
helper ×1
json ×1
syntax ×1
tdd ×1