Tag: rspec2

RSpec中无法看到ControllerMacros

我有一个Rails应用程序,我正在尝试测试它。 我使用devise登录。但是,当我想测试时,我遇到了一个问题: Users/ender/Projects/ratingw/spec/controllers/widgets_controller_spec.rb:4:in `block in ‘: undefined local variable or method `login_user’ for # (NameError) 首先,我想说我读了这个设计正式教程 。 我的spec_helper.rb是: # 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 ‘rspec/autorun’ require ‘capybara/rspec’ # Requires supporting ruby files with custom matchers and macros, etc, # […]

指定明确的’主题’的麻烦?

我正在使用Ruby on Rails 3.0.9和RSpect 2.我试图以下面的方式重构一些spec文件(为了测试更少代码类似的User类对象属性值): describe User do let(:user1) { Factory(:user, :users_attribute_a => ‘invalid_value’) } let(:user2) { Factory(:user, :users_attribute_b => ‘invalid_value’) } let(:user3) { Factory(:user, :users_attribute_c => ‘invalid_value’) } it “foreach user” do [ user1, user2, user3 ].each do |user| subject { user } it “should be whatever” user.should_not be_valid … end end end end […]

测试JavaScript重定向的麻烦

我正在使用Ruby on Rails 3.1.0和rspec-rails 2 gem。 我想测试一个JavaScript重定向,但我这样做有些麻烦。 在我的控制器文件中,我有: respond_to do |format| format.js { render :js => “window.location.replace(‘#{users_url}’);” } end 在我的spec文件中,我有: it “should redirect” do xhr :post, :create response.should redirect_to(users_url) end 如果我运行规范,我会得到以下内容: Failure/Error: response.should redirect_to(users_url) Expected response to be a , but was 如何实现规范代码以便正确测试JavaScript重定向? 解决方案的尝试 如果我使用 response.should render_template(“window.location.replace(‘#{users_url}’);”) 我明白了 Failure/Error: response.should render_template(“window.location.replace(‘#{users_url}’);”) expecting <"window.location.replace('http:///users’);”> but rendering […]

RubyTest与SublimeText 2

我正在使用Sublime Text 2并安装了RubyTest包。 我正在尝试使用Command-Shift-R运行测试。 我也使用rbenv作为ruby并使用Bundler安装了gem。 输出是: /bin/sh: rspec: command not found 控制台显示以下命令已运行: cd ‘/Users/user/Dropbox/code/app/’ && rspec spec/controllers/profile_controller_spec.rb -l 163 当我在终端中运行该命令时,它工作正常。 为什么它在Sublime中不起作用?

在RSpec 2中使用OR的平等

编写以下示例的正确方法是什么? 球员的得分应该等于5或8。 it “should equal 5 or 8” do player.score.should == 5 or 8 end 谢谢! 蒂姆

在Rakefile中设置rspec2任务

我有一个看起来像这样的Rakefile: require ‘rspec/core/rake_task’ desc “Run all RSpec tests” RSpec::Core::RakeTask.new(:spec) 这不行。 例如,如果我尝试运行“rake -T”,我得到: code/projects/bellybuster[master]% rake -T –trace (in /Users/craig/code/projects/bellybuster) rake aborted! no such file to load — rspec/core/rake_task /Users/craig/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’ /Users/craig/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’ /Users/craig/code/projects/bellybuster/Rakefile:1:in `’ /Users/craig/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:2383:in `load’ 有什么想法吗? 万一这可能对Gemfile有帮助: source :rubygems gemspec 哦和一些版本: Ruby:1.9.2p180 耙子:0.8.7 Bundler:1.0.13 RubyGems:1.7.2

rspec用户测试给出“未定义的局部变量或方法`confirmed_at’”

我的rspec测试给了我 NameError: undefined local variable or method `confirmed_at’ for # 我的用户规范是: require ‘spec_helper’ describe User do before(:each) do @user = Factory(:user) end # Factory will make sure that in the future if attributes are added the tests below don’t break # Just as long as the Factory is updated for the new attributes as appropriate. […]

如何使用rspec在ruby中模拟super?

我通过创建一个扩展到库类的子类来扩展现有的库。 在子类中,我能够在initialize方法中测试大多数function,但无法模拟super调用。 子类看起来像下面的东西。 class Child < SomeLibrary def initialize(arg) validate_arg(arg) do_something super(arg) end def validate_arg(arg) # do the validation end def do_something @setup = true end end 如何编写rspec测试(使用mocha)以便我可以模拟super调用? 请注意,我正在测试Child类中initialize方法的function。 我是否必须创建单独的代码路径,在提供额外参数时不会调用super ?

Counter_cache列的Rspec测试返回0

几天来我一直试图找到应该很容易做到的接缝的底部…然而我仍然对铁轨和ruby的世界很新,我只是不能解决这个问题。 ..:p 无论如何,我遇到的问题是我的模型中有一些:counter_cache列,这些列在手动测试时都非常好用。 但是,我想做TDD的事情,我不能缝在rspec测试他们的一些未知的原因? 无论如何这里是我的模型的一个例子(用户,评论和媒体): class User < ActiveRecord::Base has_many :comments has_many :media, dependent: :destroy end class Comment true belongs_to :user, :counter_cache => true validates :user_id, :presence => true validates :content, :presence => true, :length => { :maximum => 255 } end class Medium true has_many :comments, as: :commentable validates :user_id, presence: true validates :caption, […]

CanCan load_and_authorize_resource触发禁止的属性

我有一个使用强参数的标准RESTful控制器。 class UsersController < ApplicationController respond_to :html, :js def index @users = User.all end def show @user = User.find(params[:id]) end def new @user = User.new end def edit @user = User.find(params[:id]) end def create @user = User.new(safe_params) if @user.save redirect_to @user, notice: t('users.controller.create.success') else render :new end end def update @user = User.find(params[:id]) if @user.update_attributes(safe_params) […]