rspec

undefined method `create' rails spec.

不问归期 提交于 2019-12-21 07:55:20
问题 I have installed factory girl and trying to use it with spec. scenario 'User signs in' do create :user, email: 'test@example.com', password: 'testpassword' visit '/users/sign_in' fill_in 'Email', with: 'test@example.com' fill_in 'Password', with: 'testpassword' end and I am getting the following error. Failure/Error: create :user, email: 'test@example.com', password: 'testpassword' NoMethodError: undefined method `create' for #<RSpec::ExampleGroups::UserSignIn:0x007fe6324816b8> 回答1: We can

Rspec testing for html entities in page content

邮差的信 提交于 2019-12-21 07:30:10
问题 I'm writing a request spec and would like to test for the presence of the string "Reports » Aging Reports". I get an error (invalid multibyte char) if I put in the character in my matcher expression directly, so I tried this: page.should have_content("Reports » Aging Reports") This fails the test with the message: expected there to be content "Reports » Aging Reports" in "\n Reports » Aging Reports\n I've tried things like .html_safe with no success. Is there a way to test for text containing

Mocking ActiveRecord relationship beheavior in RSpec tests

十年热恋 提交于 2019-12-21 07:28:39
问题 I've run into this problem with testing. Let's assume I have two models, User and Post, where user has_many :posts. I'm trying to spec out a code block that includes something like this: user = User.find(123) post = user.posts.find(456) I know how to mock out the User.find and user.posts parts. The user.posts mock returns an array of Post objects. And when it get's to .find(456) part, everything breaks down with no block given exception. So my question here is: what do I return as the result

How test the send_data method in Rails?

一笑奈何 提交于 2019-12-21 07:21:44
问题 How can I test the send_data method in Rails? 回答1: When I read his question I took it to mean, how does he make sure that send_data sent the string/whatever he asked it to. Not so much to test it's sending but to ensure for his peace of mind that the method he's sent it wasn't blank. mocking, as you've done, doesn't really get him that result. Perhaps you can ensure that your string isn't blank, or something like that. This way you aren't testing send_data, but that whatever send_data gets is

How test the send_data method in Rails?

偶尔善良 提交于 2019-12-21 07:21:01
问题 How can I test the send_data method in Rails? 回答1: When I read his question I took it to mean, how does he make sure that send_data sent the string/whatever he asked it to. Not so much to test it's sending but to ensure for his peace of mind that the method he's sent it wasn't blank. mocking, as you've done, doesn't really get him that result. Perhaps you can ensure that your string isn't blank, or something like that. This way you aren't testing send_data, but that whatever send_data gets is

Preserve variable in cucumber?

时光总嘲笑我的痴心妄想 提交于 2019-12-21 07:20:07
问题 I want to access variables in difference Given/Then/When clauses. How to preserve variables so that they are accessible everywhere? Given(#something) do foo = 123 # I want to preserve foo end Then(#something) do # how to access foo at this point??? end 回答1: To share variables across step definitions, you need to use instance or global variables. Instance variables can be used when you need to share data across step definitions but only for the one test (ie the variables are cleared after each

Why do people group rspec under test and development in the Gemfile?

喜夏-厌秋 提交于 2019-12-21 06:48:48
问题 I'm generally clear on bundler Gemfile options, but I'm not sure why rspec (specifically, rspec-rails) should be in both test and development. Here are my test groupings: group :development, :test do gem 'rspec-rails' gem 'faker' end group :test do gem "factory_girl_rails" gem "capybara" gem 'guard-rspec' gem 'rb-fsevent' gem 'growl' end Does this look ok? 回答1: I am quoting the official documentation: Add rspec-rails to the :test and :development groups in the Gemfile: group :test,

What is the purpose of a `transient do` block in FactoryBot factories?

*爱你&永不变心* 提交于 2019-12-21 06:46:30
问题 What is the purpose of transient do in FactoryBot factories? I've seen a lot of factories that begin with something like below. factory :car do owner nil other_attribute nil end ... I've found some information on this blog: http://blog.thefrontiergroup.com.au/2014/12/using-factorygirl-easily-create-complex-data-sets-rails/ But I still don't fully understand how and why to do this. My experience with FactoryBot is minimal. Could anyone with some experience using FactoryBot share some insight?

RoR: how do I test my app against multiple databases?

我怕爱的太早我们不能终老 提交于 2019-12-21 06:28:45
问题 I started deploying my latest RoR app on Heroku , which required me to start using PostgreSQL -- I'd previously been using SQLite and MySQL. I wanted a dead-simple way to continually do red/green testing against all three databases to make sure I didn't break anything in the heat of development. What's a good way to do this? 回答1: @awendt kindly pointed out that I could answer my own question. It turns out the recipe is rather simple. The secret is to use a environment variable to tell Rails

Rails 4 memory profiling

拜拜、爱过 提交于 2019-12-21 06:00:32
问题 I need to profile the memory usage of a rails 4 application (using ruby MRI). A little bit of googling made me step into http://guides.rubyonrails.org/v3.2.13/performance_testing.html I have two questions : Is it compatible with rails 4 I am using rspec. It seems that my rake test tasts are not available (neither rails generator performance_test ). When I type rake test:profile for instance, I receive Don't know how to build task 'test:profile' . Does anybody know how to fix this ? Thanks in