Tag: rspec rails

在向Rails4应用程序添加授权后,RSpec控制器规范失败

在Railscast #385 & #386之后,我在Rails 4应用程序中添加了授权(rails 4.2.6,rspec-rails 3.4.2)。 添加授权后,我的所有控制器规格都会失败。 我的function规格仍然通过,因为在规范中我以admin身份登录以执行不允许访问者的操作(:edit,:update …)。 如果我允许访客进行所有操作,我的控制器规格就会通过。 但是,只有管理员才能执行的任何操作都将在控制器规范中失败。 脚手架发电机产生空的 let(:valid_session) { { } } 它在评论中说: #这应该返回应该在会话中的最小值集合,以便传递MyController中定义的任何filter(例如,身份validation)。 但是,我不知道如何配置:valid_session允许身份validation通过。 当我尝试在这个问题中发布的解决方案: def valid_session controller.stub!(:signed_in?).and_return(true) end 我收到此错误: NoMethodError: undefined method `stub’ for #<MyController 因为,逻辑的所有分支路径都应该在较低级别(即控制器规范)而不是function规格上进行测试,如何让控制器规格通过? 如何配置valid_session或我可以使用哪种方法在控制器规范中传递授权?

在rspec中别名’it’

我试图在rspec中为ROR应用程序编写一些测试(不是代码覆盖,但不相关),至少需要别名描述和它。 我可以将别名描述得很好,因为它处于最顶层。 但是,我无法得到任何其他工作。 喜欢这个家伙: module RSpec module Core class ExampleGroupMethods alias :they :it end end end 我在spec文件中包含了这个,但我没有得到正确的模块路径。 我查看了rspec代码库,但是我碰到了墙,所以我不认为我完全知道自己在做什么。 任何提示或资源将不胜感激。

如何测试RSpec中的attr_accessible字段

因此,我们通过Rails 3.2应用程序在许多字段上设置了attr_accessible和attr_protected 。 目前我们确实没有测试以确保这些字段受到保护。 所以我决定谷歌一些答案,偶然发现这个解决方案: RSpec::Matchers.define :be_accessible do |attribute| match do |response| response.send(“#{attribute}=”, :foo) response.send(“#{attribute}”).eql? :foo end description { “be accessible :#{attribute}” } failure_message_for_should { “:#{attribute} should be accessible” } failure_message_for_should_not { “:#{attribute} should not be accessible” } end 但是这个解决方案只测试方法是否响应。 我需要的是一种方法,让我测试属性可以和不能被大量分配。 老实说,我喜欢这种语法 it { should_not be_accessible :field_name } it { should be_accessible :some_field } 有没有人有更好的解决方案来解决这个问题?

如何从脚手架完成rspec put控制器测试

我正在使用脚手架来生成rspec控制器测试。 默认情况下,它会将测试创建为: let(:valid_attributes) { skip(“Add a hash of attributes valid for your model”) } describe “PUT update” do describe “with valid params” do let(:new_attributes) { skip(“Add a hash of attributes valid for your model”) } it “updates the requested doctor” do company = Company.create! valid_attributes put :update, {:id => company.to_param, :company => new_attributes}, valid_session company.reload […]

显示每个rspec示例的运行时

目前我正在运行超过1k的例子,这需要很长时间才能完成(超过20分钟!!! )。 我想确定哪些示例需要花费更多时间才能完成,有没有办法运行rspec并返回每个示例完成(单独)所需的时间? 我正在使用rspec 1.3.0和rspec-rails 1.2.3

在Windows上使用Guard和rspec更改文件时,测试不会运行

我已经安装了guard-rspec gem用于我的rails应用程序。 当我通过bundle exec guard从命令行启动后,它第一次运行我的整个测试套件没有问题。 但是,每当我对我的Guardfile中指定的spec文件或任何监视文件进行任何更改时,Guard似乎都不会识别这些更改,也不会重新运行任何测试。 我甚至尝试在我的Guardfile中放置一些显式内容,如下所示: watch(“app/views/orders/new.html.erb”) { “spec/requests/orders_spec.rb” } 当我编辑并保存new.html.erb时,应该触发我的orders_spec.rb测试运行吗? 好吧,当我编辑它并点击保存时,没有任何反应,甚至没有错误或警告。 有没有人有幸在Windows上运行Guard或遇到类似的问题?

无法使用rspec存储辅助方法

我试图在我的控制器中定义的助手上存根方法。 例如: class ApplicationController < ActionController::Base def current_user @current_user ||= authenticated_user_method end helper_method :current_user end module SomeHelper def do_something current_user.call_a_method end end 在我的Rspec中: describe SomeHelper it “why cant i stub a helper method?!” do helper.stub!(:current_user).and_return(@user) helper.respond_to?(:current_user).should be_true # Fails helper.do_something # Fails ‘no method current_user’ end end 在spec/support/authentication.rb module RspecAuthentication def sign_in(user) controller.stub!(:current_user).and_return(user) controller.stub!(:authenticate!).and_return(true) helper.stub(:current_user).and_return(user) […]

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并不能保证每次都有新的测试数据库。 这是一个错误的假设吗?

在rails 3.1上,Rspec中的Cookie不会存在

这是以下环境中控制器规范中的cookie集合的问题: rails 3.1.0.rc4 rspec 2.6.0 rspec-rails 2.6.1 我有一个简单的控制器规范,它创建一个Factory用户,调用一个设置cookie的登录方法,然后测试以查看登录用户是否可以访问页面。 问题是所有cookie似乎都在设置的身份validationcookie和我的控制器上调用的“show”操作之间消失。 在rails dev服务器上的浏览器中运行时,我的代码工作正常。 运行规范时更奇怪的行为是,通过cookie哈希设置的所有内容都会消失,但通过会话哈希设置的所有内容都会持续存在。 我只是错过了使用rspec时cookie的工作方式吗? 规格代码 it “should be successful” do @user = Factory(:user) test_sign_in @user get :show, :id => @user response.should be_success end 登录代码 def sign_in(user, opts = {}) if opts[:remember] == true cookies.permanent[:auth_token] = user.auth_token else cookies[:auth_token] = user.auth_token end session[:foo] = “bar” cookies[“blah”] = […]

关于Ruby Tutorial第9章的RSPEC错误

我在mac osx10.8上使用ruby 1.8.7。 我不知道这些错误是什么意思。 我目前正处于ruby on rails教程的第9章末尾。 任何帮助是极大的赞赏! 1) Authentication signin with invalid information Failure/Error: before { click_button “Sign in” } NoMethodError: undefined method `[]’ for nil:NilClass # ./app/controllers/sessions_controller.rb:8:in `create’ # (eval):2:in `send’ # (eval):2:in `click_button’ # ./spec/requests/authentication_pages_spec.rb:21 2) Authentication signin with invalid information Failure/Error: before { click_button “Sign in” } NoMethodError: undefined method `[]’ […]