Tag: rspec

zeus rspec失败包括必需的文件,但单独的rspec就可以了

这是一个奇怪的问题,宙斯开始顺利运行。 rspec spec /它的工作完美无瑕。 My spec_helper config is # encoding: UTF-8 require ‘rubygems’ # This file is copied to spec/ when you run ‘rails generate rspec:install’ ENV[“RAILS_ENV”] ||= ‘test’ require File.expand_path(“../../config/environment”, __FILE__) require ‘rspec/rails’ require ’email_spec’ require ‘rspec/autorun’ require ‘capybara/rspec’ require ‘shoulda-matchers’ require ‘shoulda/matchers/integrations/rspec’ Dir[“./spec/support/**/*.rb”].sort.each {|f| require f} RSpec.configure do |config| config.include FactoryGirl::Syntax::Methods config.include Devise::TestHelpers, […]

如何测试before_filter在Rails中正确使用RSpec

我的ApplicationController中有一个check_user_access_control before_filter,用于检查已记录用户的角色和权限,然后才能通过。 我正在尝试对它进行一些测试,但我找不到一个好方法。 对于简单的索引操作,我只需: it “allows access to mod” do login_as(Factory(:mod)) # this is a spec helper get :index response.code.should == “200” end 它工作得很好。 对于需要一些参数的编辑/显示/创建和其他操作,与数据库的交互以及运行后可能的重定向,它需要太多其他东西才能被存根。 有没有办法测试在before_filters之后是否调用了特定的操作? 我正在寻找像controller.should_receive(:action_name) (它不起作用)来替换response.code.should == “200”行。 版本:rails 3.0.4和rspec 2.5 我尝试了另一种方法。 我们在ApplicationController中有一个名为redirect_to_login的方法,我现在正在使用controller.should_receive(:redirect_to_login)检查并正常工作。 虽然它可以正确检测用户是否被允许,但它会存根方法,这意味着无论用户是否被允许,都会运行控制器操作。 此外,行动取决于参数和数据库,我们不希望这样。 如果现在我使用controller.stub!(:action_name)存根操作方法,则不会运行操作,但RSpec仍在查找模板。 好吧,有些动作没有模板,它们只是以redirect_to :action => :somewhere_else或render :text => “foobar” ,此时我们并不关心。 在排序中,我现在需要的是找到一种方法使RSpec 不担心模板的存在。

警卫 – 与流浪汉保持一致

我正在尝试使用guard的–listen-on选项与vagrant,如此处所述,但我无法让它工作。 如果我将config.vm.network :forwarded_port, guest: 4000, host: 4000添加到我的Vagrantfile ,然后尝试用listen -f 127.0.0.1:4000开始监听,我收到一个错误: Broadcaster.initialize: Address already in use – bind(2) for “127.0.0.1” port 4000 。 如果我试着开始听, 然后开始流浪汉,流浪汉同样抱怨: Vagrant无法转发此VM上的指定端口,因为它们会与已在这些端口上侦听的其他应用程序发生冲突。 转发到4000的端口已在主机上使用。 所以我在Vagrantfile省略了端口4000转发时尝试了其他一些事情: 如果我省略了Vagrantfile的端口4000转发,那么我可以成功开始监听listen -f 127.0.0.1:4000 。 但是当我在我的流浪客人中运行guard -o “10.0.2.2:4000” -w “/home/me/my_project/”时,当文件发生变化时,警卫不会做任何事情。 向listen调用添加-v标志表明正在主机上正确地拾取更改。 我也尝试在主持人上listen -f 10.11.12.1:4000与guard -o “10.11.12.1:4000” -w “/home/me/my_project/”在客人guard -o “10.11.12.1:4000” -w “/home/me/my_project/”同样的结果后卫不做任何事情文件更改。 将listen -f 127.0.0.1:4000与guard -o “10.11.12.1:4000” -w “/home/me/my_project/”导致后卫无法连接。 […]

在RSpec请求规范中使用Capybara时,设置自定义请求标头的最佳方法是什么?

我正在使用set_headers方法修补Capybara :: Session,该方法分配给Capybara :: RackTest :: Browser的选项属性(我已经从attr_reader更改为attr_accessor)。 补丁: class Capybara::RackTest::Browser attr_accessor :options end class Capybara::Session def set_headers(headers) if driver.browser.respond_to?(:options=) #because we’ve monkey patched it above options = driver.browser.options if options.nil? || options[:headers].nil? options ||= {} options[:headers] = headers else options[:headers].merge!(headers) end else raise Capybara::NotSupportedByDriverError end end end 在我的请求规范中,我正在做: page.set_headers(“REMOTE_ADDR” => “1.2.3.4”) visit root_path 这有效,但我想知道是否有更好的方法,只是能够在请求上设置自定义remote_ip / […]

RSpec:如何存根inheritance方法current_user(没有设计)?

我有一个基于MHartl的RoR4教程的控制器 就像MHartl一样, 我没有使用Devise ,我推出了自己的身份validation系统 因为视图调用current_user.admin?无法使用RSpec for UsersController#Edit current_user.admin? 并且控制器调用@path_switch = path_switch 我不断得到RSpec错误: 1) User Pages Edit Failure/Error: visit edit_user_path(user) NoMethodError: undefined method `admin?’ for nil:NilClass # ./app/controllers/users_controller.rb:106:in `path_switch’ # ./app/controllers/users_controller.rb:53:in `edit’ # ./spec/requests/user_pages_spec.rb:54:in `block (3 levels) in ‘ UsersController: class UsersController < ApplicationController … def edit @user = User.find(params[:id]) @path_switch ||= path_switch #error end … […]

使用shoulda重构Rails模型上的rspec测试

通过回答关于属性可访问性测试的另一个StackOverflow问题 (并认为它们非常棒)来了解shoulda-matchers后,我决定尝试重构我在The Rails Tutorial中所做的模型测试,试图使它们更加简洁和彻底。 我这样做归功于来自模块的文档的一些灵感: Shoulda::Matchers::ActiveRecord和Shoulda::Matchers::ActiveModel ,以及这个StackOverflow关于结构化应该在模型中进行测试的答案 。 但是,还有一些我不确定的事情,我想知道如何使这些测试更好。 我将使用Rails教程中的用户规范作为我的示例,因为它是最详细的,并涵盖了许多可以改进的领域。 以下代码示例已从原始user_spec.rb更改,并将代码替换为describe “micropost associations”行。 针对user.rb模型的规范测试及其工厂在factories.rb中定义。 规格/型号/ user_spec.rb # == Schema Information # # Table name: users # # id :integer not null, primary key # name :string(255) # email :string(255) # created_at :datetime not null # updated_at :datetime not null # password_digest :string(255) # remember_token :string(255) […]

Ruby Debug“没有这样的文件加载–spec_helper”

Noob可能会遗漏一些明显的东西…我正在尝试调试Rspec文件。 此时Rsspec文件被删除: require ‘spec_helper’ describe PagesController do render_views describe “GET ‘home'” do describe “when not signed in” do before(:each) do get :home end it “should be successful” do response.should be_success end it “should have a vendor section” do response.should have_selector(“h1”, :content => “Vendor”) end it “should have a hospital section” do response.should have_selector(“h1”, :content => […]

在一切都变绿之后,如何让autospec / test不运行完整的测试套件?

与twitter中的 waloeiii相同的问题: 在一切都变绿之后,如何让autospec / test不运行完整的测试套件? 我写的下一个测试将是红色的! 我宁愿手动运行完整的测试套件。 顺便说一句,我尝试添加一个失败的规范: it “should flunk” do flunk end 但是当感觉它时,autospec似乎忽略了它。

具有任何正文和标题的Webmock存根请求

如何使用Webmock与任何正文和标题存根请求? 我试着用正则表达式 WebMock.stub_request(:post, ‘api.quickblox.com/’).with(:body => /.*?/, :headers => /.*?/).to_return(:status => 201, :body => “”, :headers => {}) 在rspec但它不起作用,它有 NoMethodError: undefined method `map’ for /.*?/:Regexp

使用RSpec测试密码长度validation

我正在编写一些unit testing,以确保用户模型的密码长度不超过8个字符。 我开始使用User模型: class User { :minimum => 90, :too_short => “password is too short, must be at least %{count} characters” }, :on => :create end 和user_spec.rb测试: describe User do subject { FactoryGirl.build :user } its(:password) { should have_at_least(8).items } end 但是我意识到这实际上并没有测试我的validation,只是测试我的工厂有一个> = 8个字符的密码。 除了测试有效之外,还有一种很好的方法吗? 0-7字符密码的方法? 我的理论是,如果我只测试7个字符,并且有人意外硬编码4个字符密码就可以了,这将通过validation,但实际上不是预期的。 可能有一些代码,其中取决于密码超过8个字符(不太可能,但在其他情况下可能是真的),因此允许密码4是不正确的。 在这种情况下,在模型中更改密码validation的人将不知道他们做错了什么。 我只想知道如何使用TDD正确测试这样的情况。