I have a test using shoulda that is failing for reasons I don't understand. Any idea what the fix is for this? I hardcoded the array for testing purposes.
All my other shoulda matcher based tests are working fine.
Validation
validates_inclusion_of :status, :in => ["Active", "Closed"]
Test:
it { should ensure_inclusion_of(:status).in_array(["Active", "Closed"]) }
Failure
Failure/Error: it { should ensure_inclusion_of(:status).in_array(["Active", "Closed"]) }
["Active", "Closed"] doesn't match array in validation
Looking at the source code for that matcher:
https://github.com/thoughtbot/shoulda-matchers/blob/master/lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb#L88
Do you have another validation which prevents nil or blank values for :status?
Related
In my routes, I have the following:
match '/:org_id/users/:id/receive_message' => 'users#receive_message', :via => [:get, :post], :org_id => /[a-z0-9.-]+/i, :id => /[a-z0-9.-]+/i
In my route tests I have the following:
assert_routing({method: 'post', path: '/test4.example.com/users/user.test.email/receive_message'}, {controller: "users", action: "receive_message", org_id: "test4.example.com", id: "user.test.email"})
Which works fine. Except when I do an integration test on the action with
post "/123/users/456/receive_message"
I get a 301 redirect to http://example.com/123/users/456/receive_message
I have turned off devise authentication and authenticity_token checking for this action. It never gets to any of the before_* filters in the integration test. The action is working fine in production. Using stock Rails 4 test framework for units, controllers, and integration tests, and FactoryGirl for mocks.
Any ideas?
This testing stack is behaving unreliably. Sometimes when I run guard, it performs adequately with the guard-rspec gem and will only run the file that has been changed. At least, if the test fails it will stop with that single modified file.
But if that test passes, all bets are off. It will continue to run ALL tests when a single file is changed. On a good-sized application, this is extremely impractical and frustrating to see all tests run every time a file is saved.
My Guardfile is as follows. Notice that I had to change where it was pointing to RSpec specs. By default it thinks you are going to have a specs directory, and that all of your specs will be all jumbled into that one directory. I don't know anybody that tests like that, so it's a funny default to include. It should be looking for subdirectories under specs.
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
guard 'spork', :cucumber => false, :rspec_env => { 'RAILS_ENV' => 'test' } do
watch('config/application.rb')
watch('config/environment.rb')
watch('config/environments/test.rb')
watch(%r{^config/initializers/.+\.rb$})
watch('Gemfile')
watch('Gemfile.lock')
watch('spec/spec_helper.rb') { :rspec }
end
guard 'rspec', :version => 2, :cli => "--drb" do
watch(%r{^spec/(.*)/(.+)_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
# Rails examples
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
# Capybara features specs
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
end
The guard-rspec gem has a parameter which controls whether all tests are run after a successful test. It defaults to true, but can be changed to false to get the behavior you're looking for.
See the :all_after_pass option at https://github.com/guard/guard-rspec
For the sake of reference, the all_on_start: false and all_after_pass: false options need to be set for each guard block. It took me awhile to finally figure out why mine behaving the same way.
I've been working on Yii-based application. And I've faced with weird thing...
What I'm trying to do is to add input(type=file) into form. Form is created via form builder(CForm class). But input is not going to appear.
My code. Controller/action:
$model=MyModel::model()->findByPk(700);
$model->scenario='my-scenario';
$form=new CForm('path.to.forms.my-form', $model);
$this->render('view', array('form'=>$form));
View:
echo $form
Form config:
return array(
'attributes' => array(
'enctype' => 'multipart/form-data',
),
'elements' => array(
'name' => array(
'type' => 'text',
),
'image' => array(
'type' => 'file',
),
),
'buttons' => array(
'save' => array(
'type' => 'submit',
'label' => 'Save',
),
),
);
Model:
//.....
public $image;
// ....
public function rules()
{
return array(
//...
array('image', 'file', 'types'=>'png', 'on'=>'my-scenario'),
);
}
With code above I expected to see two fields - text and file. But only text one appears.
If I change file validator to, say, required - it works, but I need file validator.
I'm using Yii version 1.1.13.
The most intresting that code above works as expected with earlier Yii(1.1.9). Is this a known bug in new version? If yes - is there a solution? or do I have to rollback to previous version?
Thanks in advance.
UPDATE:
If you add a second validator (one for file and one for required) does it work?
No, it doesn't. I believe I found why. See bellow.
It seems to be caused by this line in CForm..
Yes, correct. Yesterday, armed with debugger I went deeper:)
CFormElement::getVisible() eventually calls CModel::isAttributeSafe() and CModel::getSafeAttributeNames().
CForm::getSafeAttributeNames() gets all model validators and leaves only safe ones. As we can see CFileValidator is not safe.
So, it doesn't matter how many safe validators(required or any other) have attribute assigned. CForm::getSafeAttributeNames() removes it from whitelist if there is at least one unsafe(file).
File validator is unsafe since 1.1.12 version. That is why it worked perfectly for me in 1.1.9 :)
Hence the problem is in CFileValidator(or at least connected with it) and not in CForm.
The only one solution I can see so far is creating own validator extended from CFileValidator marked safe and using it instead of built in. But I can't even imagine what problems it may cause(I believe Yii developers had a good reason for making it unsafe).
I hope this will be helpful for somebody.
UPDATE 2
array('image', 'file', 'safe'=>true, 'types'=>'png', 'on'=>'my-scenario')
this validation rule(explicit safe=true) also works.
If you add a second validator (one for file and one for required) does it work? I ran into this recently myself.
It seems to be caused by this line in CForm:
if($element->getVisible())
{
...
}
The getVisible checks the active validators for this current model scenario and leaves out any inputs that aren't used in the current validator rules. I've ended up commenting things out in our custom CForm model for our system, but if let me know if you find something that works better for you.
Issue has been posted on github
https://github.com/yiisoft/yii/issues/2089
I have a few tests for simple tags like or the results are correct but the rspec tests are failing. I am able to validate that the output is correct using save_and_open_page but I can't understand why the rspec tests don't view this as valid output!?
Can you spot what's wrong? Thanks!
Spec Code
it { save_and_open_page; should have_selector('title', text: "CredSimple - Sign in") }
Resulting Failure Messages
Failure/Error: it { save_and_open_page should have_selector('title', text: "CredSimple - Sign in") }
expected css "title" with text "CredSimple - Sign in" to return something
The save_and_open_page confirms that the page has the title that the test is seeking:
<title>CredSimple - Sign in</title>
I'm trying to do some Rails integration testing.
I have a case almost right out of the Rails guide on form helpers.
My HTML contains:
<input name="to_org[][str2int]" value="1" type="checkbox">
<input name="to_org[][str2int]" value="2" type="checkbox">
<input name="to_org[][str2int]" value="3" type="checkbox">
The application works fine with Rails 3.2.2.
Now I want to write an integration test. I need to post the the data the
same way my JavaScript does. I'd like to indicate "to_org[]" values 1 and
2 are selected. Assume form_url is a properly set value.
post( form_url, { "to_org[][str2int]" => ???WHAT_GOES_HERE??? } )
After the post, I'm expecting params to hold:
"to_org" => [ {"str2int" => "1"}, {"str2int" => "2"} ]
I've tried a bunch of different things, looked through the guides, googled.
Hasn't anyone tried to post the result of multiple checkboxes being
checked? I don't think the answer is obvious.
How does one set the parameters for post() in integration test to make
this work?
PS: As one might guess, I have a filter that recognizes "str2int", and
does the conversion with appropriate checking and error recovery for the
app. Eventually my app sees :to_org = [ 1, 2 ].
Basically, I did some digging through the rails code (would be nice to
see it make the API documentation.)
A right answer is:
post( form_url, { "to_org" => [ { "str2int" => "1" }, { "str2int" => "2" } ] } )
Most helpful was discovering the to_param() function, as in:
{ "to_org" => [ { "str2int" => "1" }, { "str2int" => "2" } ] }.to_param()
This allowed me to deduce the fact that I had to vary what I used as the
name to get the values passed thru post() correctly.
Rails gods: Is there a better way to do this? More that should be said?