Tag: rspec rails

为什么Rails测试环境中的机架env哈希是空的?

在我的Rails应用程序中,我正在访问我的一个控制器操作中的env哈希。 有点像: def my_before_filter env[‘some.key’] = “Something or other” end 这对我的要求非常有用。 如果我在测试环境中启动我的Rails应用程序,并访问如下操作: # /users in UsersController#index def index puts env.inspect end 然后,env哈希的内容按预期输出到控制台。 当我从RSPec示例中获取此操作时,输出是空哈希? it ‘should get the index action’ do get :index end …..{}…. # rspec output 为什么env哈希是空的? 我已经构建了一个虚拟轨道应用来展示这一点

在辅助规范中存储控制器辅助方法

在我的application_controller.rb : helper_method :current_brand def current_brand @brand ||= Brand.find_by_organization_id(current_user.organization_id) end 在我的帮助者something_helper.rb def brands return [] unless can? :read, Brand # current_brand is called end 我正在为something_helper编写一个规范,并希望将stub_brand存根 describe SomethingHelper do before :each do helper.stub!(:can?).and_return(true) # This stub works end it “does the extraordinary” do brand = Factory.create(:brand) helper.stub!(:current_brand).and_return(brand) # This stub doesnt work helper.brands.should_not be_empty end end […]

spec with guard,rails 3.1.1和ruby 1.9.3获取无法加载此类文件错误

我刚刚在macbook air上安装了rvm和所有好东西,我用ruby 1.9.3启动了我的rails 3.1.1应用程序。 在我的gem文件中,我放下了警卫和rspec,现在当我尝试单独运行rspec或使用警卫时,我会向我发出错误并且我不确定为什么, 我运行时得到的输出是这样的, phantom:tasks maxmarze$ guard Please install rb-fsevent gem for Mac OSX FSEvents support Using polling (Please help us to support your system better than that). Please install growl_notify or growl gem for Mac OS X notification support and add it to your Gemfile Guard is now watching at ‘/Users/maxmarze/rails_projects/tasks’ Guard::RSpec […]

Rspec生成的规范失败了

我已将以下内容添加到application.rb文件中 config.generators do |g| g.test_framework :rspec, :fixtures => true, :view_specs => false, :helper_specs => false, :routing_specs => false, :controller_specs => true, :request_specs => true g.fixture_replacement :factory_girl, :dir => “spec/factories” end 然后我生成了一个控制器 $rails g controller home index 这生成了spec / controllers / home_controller_spec.rb中的规范,如下所示 describe HomeController do describe “GET ‘index'” do it “returns http success” do get ‘index’ […]

FactoryGirl创建了不完整的模型

假设我有一个城市模型,其中: class city field :full_name, type: String # San Francisco, CA, United States field :_id, type: String, overwrite: true, default: ->{ full_name } end 假设我在/spec/factories/cities.rb中定义了一个工厂: FactoryGirl.define do factory :city do full_name ‘San Francisco, CA, United States’ end end 在其中一个规范中运行以下代码: city_attrs = { full_name: ‘San Francisco, CA, United States’ } City.create! city_attrs => # FactoryGirl.create(:city) => […]

如何在工厂中使用’sequence’来避免使用’FactoryGirl.reload’?

必须使用FactoryGirl.reload可能会花费一些开销来运行所有测试(当存在许多测试时),并且它也被描述为反模式 。 如何继续使用唯一字段的排序,然后测试正确的值? 你可以在我的代码中看到…… 我必须调用FactoryGirl.reload以便在任何先前的测试已经运行的情况下,增量从1开始。 我必须声明:count => 1因为只有一个该值的实例。 规格/工厂/ clients.rb FactoryGirl.define do factory :client do name “Name” sequence(:email} { |n| “email#{n}@example.com” } sequence(:username) { |n| “username#{n}” } end end 规格/客户/ index.html.erb_spec.rb require ‘spec_helper’ describe “clients/index” do before do FactoryGirl.reload (1..5).each do FactoryGirl.create(:client) end @clients = Client.all end it “renders a list of clients” do render […]

RSpec与设计

我刚接触到rails :)我试图运行我的第一个测试。 为什么这个测试通过? 用户名应该至少有2个字符,我的用户名有更多,它仍然通过测试。 user.rb: validates :username, :length => { :minimum => 2 } user_spec.rb require ‘spec_helper’ describe User do before do @user = User.new(username: “Example User”, email: “user@example.com”, password: “foobar”, password_confirmation: “foobar”) end describe “when name is not present” do before { @user.username=”aaaahfghg” } it { should_not be_valid } end end

如何使用rspec为具有自定义属性的序列化程序编写unit testing

嗨,我正在使用ruby-2.5.0和rails 5进行RoR项目。我正在为我的api使用jsonapi-serializers。 我有一个自定义属性的序列化程序如下: – class ReceiptPartialSerializer include JSONAPI::Serializer TYPE = ‘receipt’ attribute :id attribute :receipt_partials attribute :receipt_partials do receipt_container = [] object.receipt_partials.each do |partial| receipt_partial = {} receipt_partial[‘id’] = partial.id receipt_partial[‘image_url’] = ‘https:’ + partial&.picture&.url receipt_container << receipt_partial end receipt_container end end 我的序列化程序的spec文件是: – RSpec.describe ReceiptPartialSerializer do let(:id) { 1 } let(:image_url) { ‘https:/images/original/missing.png’ } […]

Rails RSpec请求规范失败,因为意外的%2F添加到重定向响应

在config / routes.rb中: get ‘books(/*anything)’ => redirect( “/public/%{anything}” ), :format => false 在spec / requests / store_request_spec.rb中: get ‘/books/aoeu/snth’ expect(response).to redirect_to( ‘/public/aoeu/snth’ ) 这失败了: Failure/Error: expect(response).to redirect_to( ‘/public/aoeu/snth’ ) Expected response to be a redirect to but was a redirect to . Expected “http://www.example.com/public/aoeu/snth” to be === “http://www.example.com/public/aoeu%2Fsnth”. # ./spec/requests/store_request_spec.rb:14:in `block (3 levels) in ‘ […]

FactoryGirl – validation失败:时区未包含在列表中

我正在使用Rspec,FactoryGirl和Capybara。 使用ActiveSupport :: TimeZone.us_zones时。 我为我的requests/users_spec运行我的测试,它甚至没有达到测试,因为它在工厂有问题。 这是错误: Failure/Error: make_user_and_login ActiveRecord::RecordInvalid: Validation failed: Time zone is not included in the list # ./spec/support/user_macros.rb:3:in `make_user_and_login’ # ./spec/requests/users_spec.rb:7:in `block (3 levels) in ‘ 这是我的设置: 应用程序/模型/ user.rb class User /^(?=(.*[a-zA-Z]){3})[a-zA-Z0-9]+$/ validates_uniqueness_of :email validates_uniqueness_of :username, :case_sensitive => false validates_length_of :username, :within => 3..26 validates_inclusion_of :time_zone, :in => ActiveSupport::TimeZone.us_zones end 投机/ spec_helper.rb […]