Tag: rspec

Hartl的Rails教程第9章练习6

更新,显示和删除用户,练习 有没有办法为用户控制器操作创建Rspec测试,例如“创建”和“新建?” 我不太清楚两个行为“创造”和“新”之间的差异; 请问有人可以这么善意吗? 创建测试后,我将如何实现redirect_to root_path? 我想我应该在before_filter signed_in部分中包含“new”和“create”操作,但这不会自动重定向到根目录。 我尝试通过修改users_controller.rb文件来传递测试,如下所示: def create if signed_in? redirect_to root_path else @user = User.new(params[:user]) if @user.save sign_in @user flash[:success] = “Welcome to the Sample App!” redirect_to @user else render ‘new’ end end end

用于创建属性“货币”的弃用警告

我正在使用Rails 3.2.3和money-rails gem,我有一个产品型号,其中包含以下内容: 我的模特 class Product “Money”, :mapping => [%w(price_cents cents), %w(currency currency_as_string)], :constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) }, :converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, “Can’t convert #{value.class} to Money”) } end 我的测试 require ‘spec_helper’ describe Product do context “testing money gem” do it […]

Rails:如何解决’rake / rdoctask”已弃用’警告?

只是预警:我是铁杆菜鸟。 当我跑: rake db:migrate 我收到了这个弃用警告: WARNING: ‘require ‘rake/rdoctask” is deprecated. Please use ‘require ‘rdoc/task’ (in RDoc 2.4.2+)’ instead. at /Users/username/Code/rails/appname/rake/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/rdoctask.rb 我正在使用: Rails 3.0.1 耙0.9.2.2 RSpec 2.0.1 RDoc 3.12 如果我卸载rake 0.9.2.2并使用0.8.7没有警告,但我宁愿不把它算作解决方案。 谷歌搜索后,许多网站说我需要更新我的Rakefile中的一行(基本上需要’rake / rdoctask’来改变需要’rdoc / task’)。 但是,我的Rakefile看起来像这样: require File.expand_path(‘../config/application’, __FILE__) require ‘rake’ AppName::Application.load_tasks 没有要求替换的声明。 当我添加require’rdoc / task’时,它没有任何效果。 当我在项目中搜索已弃用的’rake / rdoctask’时,没有结果。 为什么rails抱怨? 编辑:不确定是否重要,但这是我的gemfile: source ‘http://rubygems.org’ gem ‘rails’, […]

在Rails引擎规范中使用正确的url_for方法

我在Rails引擎中有一个请求规范。 呈现的视图调用路径并传入散列,即projects_path(:scope => “user”) 。 像这样的路由最终将调用url_for ,但url_for在很多地方定义。 在主应用程序(根级别)中运行应用程序或运行请求规范时,调用链最终在ActionView::RoutingUrlFor#url_for ; 但是,当在引擎中运行规范时,调用链最终会出现在ActionView::Helpers::UrlHelper#url_for 。 在Rails 4中, UrlHelper定义的url_for方法不再接受哈希参数,所以我留下了这个错误 Failure/Error: visit projects_opportunity_intakes_path ActionView::Template::Error: arguments passed to url_for can’t be handled. Please require routes or provide your own implementation 我想弄清楚的是,为什么当我在我的引擎中运行规范时,它会回到ActionView::Helpers::UrlHelper ,但在其他任何情况下都没有。 这非常令人困惑。 有一件事对我来说很突出:当我在视图中放置一个断点并评估self.class ,我通常得到一些从ActionView::Baseinheritance的东西,但是当我从我的引擎运行一个规范时,该类不会inheritance从任何事情。 我不知道这是否相关,但看起来很可疑。 有人经历过吗? 知道解决方法吗? 我的模块可能被包含在错误的订单中吗? 在spec_helper或虚拟应用程序初始化中有什么东西我可以做以确保正确使用正确的模块吗?

自定义设计会话控制器的RSpec测试因AbstractController :: ActionNotFound失败

我目前正在尝试使用rspec测试自定义Devise会话控制器。 我的控制器看起来像这样: class SessionsController < Devise::SessionsController def create #valid email? if !(params[:email] =~ /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/) set_flash_message :notice, "Please enter a valid e-mail address!" end super end end 我的RSpec控制器测试是这样的: require ‘spec_helper’ require ‘devise/test_helpers’ describe SessionsController do it “should put a warning on invalid mail address login attempt” do post :create, :user => {:email => ‘invalidEmailAddress’} response.should contain […]

添加I18n翻译到rspec测试

如何在我的规格中添加翻译测试? 就像是 : flash[:error].should == I18n.translate ‘error.discovered’ 这当然不起作用。 如何使其工作? 我想确保我收到一些错误。

Rails 3教程第11章“validation失败:已经收到电子邮件”错误

我的麻烦出现在Ruby on Rails Tutorial的第11章中。 我看到这个rspec错误: Failure/Error: :user => Factory(:user, :email => Factory.next(:email))) ActiveRecord::RecordInvalid: Validation failed: Email has already been taken 首先在user_spec.rb然后在micropost_spec.rb 。 这真是令人费解。 我认为每次autotest运行rspec时,工厂语句都在一个新的测试数据库中生成一个用户。 我用git检查了主分支中的源文件并再次尝试,但看到了同样的错误。 因此我怀疑它与db内容有关,而不是代码。 所以,我做了以下事情: restarted “rails s” restarted autotest rake db:reset rake db:migrate rake db:test:prepare rake db:populate ……一切都变绿了。 rspec测试通过了。 可能有一个更“重要”的解决方案,但我很激动这个工作。 希望它可以帮助别人。 我只能得出结论,我的测试/开发以某种方式在数据库中添加了一些意想不到的东西。 我想上面的步骤是在第11章结尾附近让自己成为新数据库的好方法。 有没有更直接的方法来解决这个问题? 错误是否表明我在没有意识到的情况下解决了其他问题? 我认为运行rspec并不能保证每次都有新的测试数据库。 这是一个错误的假设吗?

如何为简单的PUT更新编写RSpec测试?

我正在努力巩固我对rails和BDD工作流程的理解,所以我想通过创建其中一个迷你博客来开始,但是使用rspec。 现在我有一个ArticlesController和Article模型,以及相关的rspec文件。 文章非常简单,只有标题:字符串和内容:文本,而ArticlesController是RESTful – 虽然我手写了MCV for Article,但它基本上和我用脚手架创建它一样。 但是,当我在rspec中为PUT更新编写测试时,我真的不知道我在做什么。 我正在使用Factory Girl来创建文章对象,到目前为止我的代码看起来像: #factories.rb FactoryGirl.define do factory :article do title “a title” content “hello world” end #articles_controller_spec.rb before(:each) do @article = Factory(:article) end describe “PUT ‘update/:id'” do it “allows an article to be updated” do @attr = { :title => “new title”, :content => “new content” } put […]

测试使用Devise和RSpec的视图

我想在添加Devise的user_signed_in?之后获得之前传递的rspec“视图规范” user_signed_in? 有问题的视图模板的方法。 模板看起来像这样: Welcome back. Please sign in. 传递的视图规范看起来像这样: require “spec_helper” describe “home/index.html.erb” do it “asks you to sign in if you are not signed in” do render rendered.should have_content(‘Please sign in.’) end end 添加对user_signed_in?的调用后产生的错误user_signed_in? 是: 1) home/index.html.erb asks you to sign in if you are not signed in Failure/Error: render ActionView::Template::Error: undefined method […]

如何删除unit testing并用rspec替换它?

我有一个rails应用程序,使用默认测试单元进行设置,如何删除它并使用rspec代替? (我没有写任何测试,我只是在应用程序生成期间没有忽略它)。