Tag: railstutorial.org

如何使用RSpecs valid_session?

我在RSpec中找到valid_session的例子时遇到了麻烦,现在所有的脚手架测试在添加授权后都被破坏了。 通过Michael Hartl的Rails教程,我正在使用此处描述的身份validation,除了我要求所有页面的登录并使用’skip_before_filter’进行登录等。 我添加了一个用户夹具,我已validation它已加载: userexample: id: 1 name: Example Name email: examplename@example.com password_digest: $2a$10$NuWL8f2X0bXaCof3/caiiOwlF2rago7hH10JECmw1p75kEpf0mkie remember_token: YXjPrFfsK8SFQOQDEa90ow 然后在控制器规范中 def valid_session { remember_token: “YXjPrFfsK8SFQOQDEa90ow” } end 会话助手的代码 module SessionsHelper def sign_in(user) cookies.permanent[:remember_token] = user.remember_token self.current_user = user end def signed_in? return !current_user.nil? end def sign_out self.current_user = nil cookies.delete(:remember_token) end def current_user=(user) @current_user = user end def […]

为什么在保存对象后使用’reload’方法? (Hartl Rails Tut 6.30)

我正在为Hartl的Rails 4教程第6章练习。 第一个练习测试以确保用户电子邮件地址正确缩小: require ‘spec_helper’ describe User do . . . describe “email address with mixed case” do let(:mixed_case_email) { “Foo@ExAMPle.CoM” } it “should be saved as all lower-case” do @user.email = mixed_case_email @user.save expect(@user.reload.email).to eq mixed_case_email.downcase end end . . . end 我不明白为什么这里需要’重载’方法。 一旦@user.email设置为@user.email的内容并保存 ,是不是@user.reload.email和@user.email同样的事情? 我把重装方法拿出去尝试它,它似乎没有改变任何测试。 我在这里想念的是什么?

使用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) […]

未定义的方法`save’使测试在测试结束前失败

我关注ruby.railstutorial.org。 我遇到了一些麻烦,但我解决了。 然而,现在,我正在谷歌搜索相当一段时间,检查代码,我甚至知道为什么测试失败,但不知道如何使它通过。 所以,这就是问题所在。 我有一个用户模型: class User < ActiveRecord::Base attr_accessible :email, :name validates :name, presence: true, length: {maximum: 50 } VALID_EMAIL_REGEX = /\A[\w+\-.]+@[az\d\-.]+\.[az]+\z/i validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } end 该问题与不区分大小写的唯一性检查有关。 Rspec的测试是: before { @user = User.new(name: “Example User”, email: “user@example.com”) } subject { @user } describe […]

RSpec测试破坏方法(Rails Tutorial 3.2 Ch.9,Ex.10)

注意:我已经阅读了这个问题和答案,但由于某种原因,代码对我不起作用。 (见下面我得到的错误) Rails教程第9章练习10要求您:修改[针对用户]的销毁操作,以防止管理员用户破坏自己。 (先写一个测试。) 这里棘手的部分是测试它,因为应用程序已经为当前用户隐藏了“删除”链接,因此您必须直接执行http请求。 我让代码工作,并通过删除隐藏当前用户的删除链接的代码来测试它。 果然,如果我点击当前登录用户的删除链接,它会重定向我并给我通知消息。 来自users_controller.rb def destroy @user = User.find(params[:id]) if current_user?(@user) redirect_to users_path, notice: “You can’t destroy yourself.” else @user.destroy flash[:success] = “User destroyed.” redirect_to users_path end end 我遇到的问题是在编写测​​试时会发送删除请求并调用destroy方法。 我试过Rspec测试的解决方案, 如果没有删除链接 ,我将在这里复制: 来自user_pages_spec.rb describe “destroy” do let(:admin) { FactoryGirl.create(:admin) } it “should not allow the admin to delete herself” do sign_in […]

Bootstrap 3导航栏链接应显示垂直时应为水平

按照Michael Hartl的ruby on rails教程并在将bootstrap导入custom.css.scss文件后,链接显示为一个块(垂直),当它们真的应该是水平的时,导致导航栏真的很厚,如下所示。 所以问题是我做错了什么以及如何使用boostrap使导航水平? 这是我的application.html.erb布局 true %> true %> 这是我的Gemfile source ‘https://rubygems.org’ ruby ‘2.0.0’ #ruby-gemset=railstutorial_rails_4_0 gem ‘rails’, ‘4.0.2’ group :development, :test do gem ‘sqlite3’, ‘1.3.8’ gem ‘rspec-rails’, ‘2.13.1’ gem ‘guard-rspec’, ‘2.5.0’ gem ‘spork-rails’, ‘4.0.0’ gem ‘guard-spork’, ‘1.5.0’ gem ‘childprocess’ end group :test do gem ‘selenium-webdriver’, ‘2.35.1’ gem ‘capybara’, ‘2.1.0’ gem ‘growl’, ‘1.0.3’ end gem […]

Rspec测试应该通过但是失败

我从迈克尔哈特书中得到了这个测试: require ‘spec_helper’ describe “Static pages” do let(:base_title) { “Ruby on Rails Tutorial Sample App” } describe “Home page” do it “should have the h1 ‘Sample App'” do visit ‘/static_pages/home’ page.should have_selector(‘h1’, :text => ‘Sample App’) end it “should have the title ‘Home'” do visit ‘/static_pages/home’ page.should have_selector(‘title’, :text => “#{base_title} | Home”) end end […]

Hartl对config.serve_static_files的sample_app警告,并且已经定义了测试

我正在尝试测试Hartl的sample_app,这是我在运行bundle exec rake test后得到的消息: DEPRECATION WARNING: The configuration option `config.serve_static_assets` has been renamed to `config.serve_static_files` to clarify its role (it merely enables serving everything in the `public` folder and is unrelated to the asset pipeline). The `serve_static_assets` alias will be removed in Rails 5.0. Please migrate your configuration files accordingly. (called from block in at […]

railstutorial.org,第6章。未知属性:密码

我已经完成了railstutorial的第6章,但是在我添加了password和password_confirmation以及以下错误后,我的所有用户模型规范都开始失败: Failures: 1) User Failure/Error: @user = User.new(name: “Example User”, email: “user@example.com”, ActiveRecord::UnknownAttributeError: unknown attribute: password # ./spec/models/user_spec.rb:17:in `new’ # ./spec/models/user_spec.rb:17:in `block (2 levels) in ‘ 2) User Failure/Error: @user = User.new(name: “Example User”, email: “user@example.com”, ActiveRecord::UnknownAttributeError: unknown attribute: password # ./spec/models/user_spec.rb:17:in `new’ # ./spec/models/user_spec.rb:17:in `block (2 levels) in ‘ 3) User Failure/Error: @user = […]

如何为Hartl的rails教程授予AWS用户权限

我不知道如何(或在何处)从AWS授予用户读写权限,以便用户可以在生产环境中的sample_app上发布图片。 这是第11章的最后一项任务,它没有被教程所涵盖,我无法在任何地方找到解决方案。 这是carrier_wave.rb文件: if Rails.env.production? CarrierWave.configure do |config| config.fog_credentials = { # Configuration for Amazon S3 :provider => ‘AWS’, :aws_access_key_id => ENV[‘lalala’], :aws_secret_access_key => ENV[‘oloalle’] } config.fog_directory = ENV[‘name of bucket’] end end 这是教程中的过程:1)创建AWS IAM用户并记录访问权限和密钥 – 完成 2)创建S3桶 – 完成 3) 授予对上一步创建的用户的读写权限 – 如何??? 4)然后我运行这三个命令: $ heroku config:set S3_ACCESS_KEY=lalala $ heroku config:set S3_SECRET_KEY=oloalle $ heroku […]